1, garbage collection mechanism only acts on heap memory, and is not related to stack memory;
2, Stack: Stack stacks are faster to access than heaps, and high efficiency saves reference values for local variables and objects
3, heap: Saving large variables
4, the compiler handles int a = 3 o'clock, first creates a reference to the variable A in the stack , and then finds out if there is a value of 3 in the stack, and if it does not, it stores 3 in it;
Handling B = 3 o'clock is also the case, since the 3 was previously stored in, so the compiler simply points to the creation of the B variable to 3 (which facilitates space saving).
At this point A and B are at the same time pointing to 3, but do not affect their use. If you define a = 4 at this point, you need to store 4 in memory, and the change from a to 4,a value will not affect the B value.
Partition of Memory: 1, register. 2, local method area. 3, method area. 4, Stack memory. All stored are local variables. And once the scope of the variable belongs to the end, the variable is automatically freed. 5, heap memory. Storage is an array and an object (in fact an array is an object) that is usually built in the heap. Features: 1, each entity has an initial address value. 2, each variable in the heap memory has a default initialization value, which differs depending on the type. The integer is 0, decimal 0.0, or 0.0f,boolean false char ' \u0000 ' 3, garbage collection mechanism.
Java Stack and heap differences