Java-memory mechanism
Java divides memory into two types: stack memory and heap memory. Some of the basic types of variables defined in the function and the referenced variables of the object are allocated in the function stack memory. When a variable is defined in a block of code, java allocates memory space for this variable in the stack. When the scope of the variable is exceeded (for example, function B is called in function A, and variable a is defined in function B, the scope of variable a is only function B. After function B runs, variable a is automatically destroyed. The memory allocated to it will be recycled). Java will automatically release the memory space allocated for the variable, which can be immediately used as another.
Heap memory is used to store the objects and arrays created by new, and 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, this variable in the stack becomes an array or object reference variable. Later, you can use the reference variable in the stack in the program to access the array or object in the stack, 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 beyond the code block where new statements are 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. In fact, the variables in the stack point to the variables in the heap memory, which is the pointer in Java!
Code instance Test01: create a single object
An object per is instantiated in the above program. During the Object Instantiation process, space needs to be opened in the memory, which includes the stack memory and the memory. The specific memory allocation is shown in:
We can find that the object name per is saved in the stack memory (more accurately, the stack memory stores the access address of the heap memory space ), the specific content of the object, such as the attribute name and age, is stored in the heap memory. Because the per object is only instantiated and has not been assigned a specific value, it is the default value. The default value of the string is null, and the default value of the int type is 0. As mentioned above, the new keyword must be used to open up heap memory space. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPjxzdHJvbmc + tPrC68q1wP1UZXN0MDKjurbguPa21M/paper "write image description here" src = "http://www.bkjia.com/uploads/allimg/160110/042QM5L-2.png" title = "\"/>
Key Concept: A class is a reference type like an array. A reference type means that a heap of memory can be pointed to by multiple stacks of memory at the same time. Let's take a look at the simple example of reference transfer.
Code instance Test03: Object Reference transfer 1
The program running result is:
Name: Zhang San, age: 33
Name: Zhang San, age: 33
From the running results of the program, we can find that the two objects output the same content. In fact, the so-called reference transfer means that the right to use one heap memory space is transferred to multiple stack memory spaces, each stack memory space can modify the content of the heap memory space. The memory distribution diagram of this program is as follows:
Note: In the above instance, object per2 does not have heap memory space, because object per2 only declares and does not instantiate. Memory space is available only after the new keyword is instantiated.
Code instance Test04: Object Reference transfer 2
The program running result is as follows:
Name: Zhang San, age: 30
Name: Zhang San, age: 30
From the output results of the program, we can find that Test03 is the same. However, the memory allocation has changed as follows:
Java provides the Garbage collection mechanism (Garbage
Collection, GC), will occasionally cast unnecessary memory space, as long as the object is not used, will wait for GC to release space, such as the above heap memory name = "Li Si"; age = 33. A stack memory can only point to one pair of memory space. If you want to point to another stack memory space, you must first disconnect the existing point to allocate a new point.
Common memory areas in java
There are mainly four Memory Spaces in java. These memory names and functions are as follows:
Stack memory space: stores all object names (more accurately, stores the address of the referenced heap memory space). heap memory space: stores the specific attribute content of each object. Global data zone: stores static attributes. Global Code area: stores all method definitions.