The java stack and heap differences are described in detail

  • 2020-05-26 08:25:29
  • OfStack

java stack and heap differences

1, garbage collection mechanism only ACTS on heap memory, and has nothing to do with stack memory;

2. Stack: the access speed of stack stack is faster than that of the heap, and it saves the reference values of local variables and objects efficiently

3. Heap: save large variables

4. There is a very important particularity of the stack, that is, the data in the stack can be Shared

When the compiler processes int a = 3, it first creates a reference to a in the stack, and then looks for the value 3 in the stack. If it doesn't, it stores 3 in the stack.

The same is true for b = 3, where the compiler simply creates the b variable and points it to 3 (to save space) because it stored the 3.
This is when a and b point to 3 at the same time, but does not affect their use. If I define a = 4; , then 4 needs to be stored in memory and pointed from a to 4. The modification of a value will not affect b value.


 Memory partition: 


1 Register. 
2 , the local method area. 
3 , method area. 
4 , stack memory. 
  All you store are local variables. 
  And the scope of the variable 1 Once finished, the variable is automatically released. 
5 , heap memory. 
  The storage is arrays and objects ( An array is an object )  Anyone who new Build in the heap. 
  Features: 
 1 , each 1 Each entity has a first address value. 
 2 , each in heap memory 1 Each variable has a default initialization value, depending on the type. The integer is 0 , the decimal 0.0 or 0.0f . boolean false char '\u0000'
 3 , garbage collection mechanism. 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: