The difference between java stack and heap
1. The garbage collection mechanism only acts on heap memory and has nothing to do with stack memory;
2. Stack: The stack is faster to access than the heap and has high efficiency to save reference values of local variables and objects.
3. Heap: Save larger variables
4. There is a very important special feature of the stack, that is, the data that exists in the stack can be shared.
When the compiler processes int a = 3, it will first create a reference to the variable a in the stack, and then find out whether the value 3 exists in the stack. If it does not exist, 3 will be stored;
The same is true when processing b = 3, because 3 was stored in before, so the compiler only needs to point the variable created b to 3 (which is conducive to saving space).
At this time, a and b point to 3 at the same time, but it does not affect their use. If a = 4; is defined at this time, 4 needs to be stored in memory, pointing from a to 4, and the modification of a value will not affect the value b.
Memory division: 1, register. 2. Local method area. 3. Method area. 4. Stack memory. All stored local variables. Moreover, once the scope to which the variable belongs is ended, the variable will be automatically released. 5. Heap memory. Storage is an array and an object (in fact, an array is an object). Any new is built in the heap. Features: 1. Each entity has a home address value. 2. Each variable in heap memory has a default initialization value, which varies according to the type. Integer is 0, decimal 0.0 or 0.0f, boolean false char '/u0000' 3, garbage collection mechanism.
Thank you for reading, I hope it can help you. Thank you for your support for this site!