Java program running mechanism and running process

Source: Internet
Author: User

Java operating mechanism

Java Virtual machine: the Java Virtual machine can be understood as a byte-code machine instruction CPU, for different operating platforms, there are different virtual machines, Java virtual machine mechanism masks the difference between the underlying operating platform, really implemented a "one-time compilation, Run anywhere. "

Java garbage collection (garbage Collection): Unused memory space should be recycled; in languages such as C + +, the programmer is responsible for reclaiming useless memory; the Java language eliminates the programmer's duty to reclaim useless memory, providing a system-level thread to track the allocation of storage space And, when the JVM is idle, checks for and frees the memory space that can be freed, and garbage collection is automatically carried out during Java programs, and programmers cannot control and intervene.

Java Run process

Java source file (*.java)-->java compiler--bytecode file (*.class)--Class loader--byte-code calibration-interpreter--operating system (Windows, Linux, etc.) the entire file load into the memory area, after a series of actions to form the operating system to understand the code, the operating system found the main method to start.

Heap-->new out of this place.

Stack--> Local Variables

Data segment--> static variable or string constant

Code segment--> storing codes

Local variables: Methods or variables defined inside a statement block (local variable)

Member variables: External to the method, variables defined inside the class, also called global variables (Glbal valiable)

Java Application Preliminary

There can be at most one public class in a source file, with no limit to the number of other classes. The entrance to the program is:

public static void Main (string[] args) {...}

The Java language is strictly case-sensitive and formatted.

Program execution process and memory allocation in Java:

The following is a program example to illustrate the allocation of memory, the program is as follows:

Note: The heap and stack in the picture below should be interchanged, I have reversed the drawing!

1. First define an object test with new, (to define an object or variable with new) its memory allocation is as follows:

2. Then define a date for the shaping variable with a value of 9. Memory allocations are as follows:

3. Then two objects, D1 and D2, are defined with new. The memory allocations are as follows:

4. Then use test to call its method Change1, called the method, and passed the value. The 9 is assigned to the variable I at the time of invocation, and the value of the variable i is changed only after the body, without changing the value of date. The memory allocation situation is as follows:

5. Since the variable i is allocated when the function change is called, the memory allocation of the variable i is recycled at the end of the function call. After the call, the memory allocation situation is as follows:

6. Call the Change2 function of test and pass the D2 as a parameter to the formal parameter. Allocates an object B's space in the memory heap when the function is called, and then assigns the actual cells and values in the memory stack by value passing. When calling Change2, executing the statement in the body of the function only changes the content in object B, and the content in D1 does not change. The memory allocation situation is as follows:

7. After the function is called, reclaim the memory space of B (including the contents of the heap and the stack). The memory allocation situation is as follows:

8. Call test method Change3 method, allocate a chunk of memory in the heap to the Parameter object B, the allocation in the stack points to the D1 region, the memory allocation is as follows:

9. Then execute the statement in the function, change the value in object D1 to 22, and after the call function ends, reclaim the memory space in the stack. The memory allocations are as follows:

Note: The heap and stack in the above image should be interchanged, I have reversed the drawing!

In summary, the memory allocation situation is:

The 1.new assigned variable is in the region, and the local variable is in the Stack area.

2. If you want to change the value, only if its method is called by reference.

When the program executes, the memory situation is very important, only to understand the memory allocation situation, can be good to solve the problem.

Java program running mechanism and running process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.