Java program execution memory processing process, java program execution memory
We need to know not only what we learn, but also why. Your salary must be in direct proportion to your learning depth.
In the past, we learned how to develop a program and have some development experience. But how does a program run in the memory?
1. First, let's look at a picture.
2. In this figure, we can clearly understand:
- The stack mainly stores local variables.
- Store new items in the heap.
- Static variables or string constants exist in the data segment (data zone;
- The method in the class exists in the code segment.
3. Example
Give a simple java code example for your understanding
public static void main(String args[]){ Point p = new Point(1, 2, 3);}class Point{ int x, y, z;Point(int _x, int _y, int _z) x = _x; y = _y; z = _z; }
The running process of this program is as follows:
- Load the program from the hard disk to the memory area
- Locate the main method and start execution.
- Memory Management During Program Execution
The local variable p appears in the stack memory;
The data zone appears in the heap memory;
The p pointer in the stack points to the corresponding data zone in the heap ..
What is the execution process of java programs in the memory? Can I recommend a book? My email: weiclear99 @ gmailcom
I have a space log, but I cannot access it without adding a friend. paste it directly.
Java memory allocation and management is one of the core technologies of Java. We have previously introduced Java memory management and Memory leakage and Java garbage collection knowledge. Today we go deep into Java core again, this section describes Java's knowledge about memory allocation in detail. Java generally involves the following areas in memory allocation:
◆ Register: we cannot control it in the program
◆ Stack: stores basic types of data and object references, but the object itself is not stored in the stack, but stored in the heap.
◆ Heap: stores data generated with new
◆ Static domain: static members defined by static in objects
◆ Constant pool: stores constants.
◆ Non-RAM storage: Permanent storage space such as hard disk
Stack in Java Memory Allocation
Variable data of some basic types defined in functions and referenced variables of objects are allocated in the function stack memory.
When a variable is defined in a code block, Java allocates memory space for the variable in the stack. After the variable exits the scope, java will automatically release the memory space allocated for the variable, and the memory space can be used for another use immediately.
Heap in Java Memory Allocation
Heap memory is used to store objects and arrays created by new. The memory allocated in the heap is managed by the Java Virtual Machine's automatic garbage collector.
After an array or object is generated in the heap, you can define a special variable in the stack so that the value of this variable in the stack is equal to the first address of the array or object in the heap memory, the variable in the stack becomes the referenced variable of the array or object. The referenced variable is equivalent to an array or an object name. Later, you can use the referenced variable in the stack in the program to access the array or object in the heap. The referenced variable is equivalent to an array or object name.
The referenced variable is a common variable. It is assigned to the stack during definition. The referenced variable is released after the program runs out of its scope. Arrays and objects are allocated in the heap. Even if the program runs outside the code block where the new statement is used to generate arrays or objects, the memory occupied by arrays and objects is not released, arrays and objects become junk only when no referenced variable points to it. They cannot be used, but still occupy the memory space, the garbage collector collects (releases) the garbage collector at an uncertain time ). This is also the reason why Java accounts for memory usage.
In fact, the variables in the stack point to the variables in the heap memory. This is the pointer in Java!
Constant pool)
A constant pool refers to some data that is determined during the compilation period and stored in the compiled. class file. Besides containing the basic types (such as int and long) defined in the Code and the constant values (final) of the object type (such as String and array) it also contains some symbolic references that appear in the form of text, such:
◆ Full qualified names of classes and interfaces;
◆ Field name and descriptor;
◆ Method and name and descriptor.
The virtual machine must maintain a constant pool for each mounted type. A constant pool is an ordered set of constants used for this type, including direct constants (string, integer, and floating point constants) and symbolic references to other types, fields, and methods.
For a String constant, its value is in the constant pool. The constant pool in JVM exists in the memory as a table. For the String type, there is a fixed-length CONSTANT_String_info table to store the text String value. Note: this table only stores the text string value, not the symbol reference. Here, we should have a clear understanding of the storage location of string values in the constant pool.
During program execution, the constant pool is stored in the Method Area rather than in the heap.
Stack and stack
The Java heap is a runtime data zone and class (the object allocates space from it. These objects are created using commands such as new, newarray, anewarray, and multianewarray. They do not need program code to be explicitly released. The heap is responsible for garbage collection. The advantage of the heap is that the memory size can be dynamically allocated and the lifetime does not need to be told to the compiler in advance, because ...... the remaining full text>
How to release the memory used in JAVA program running
The memory in Java is automatically recycled, and you do not need to explicitly call the release. To do this, you can use the jconsole program under JDK to connect to the Java Virtual Machine and then perform GC operations.
In addition, the longer it takes, the more memory it occupies, the more code problems it should be.
It cannot be solved. You can increase the maximum memory of the virtual machine, or restart it regularly.