Heap (Stack) (FIFO):
The heap is a run-time data area from which the object of the class allocates space. These pairs are created through directives such as new, NewArray, Anewarray, and Multianewarray , and they do not require program code to be explicitly released. Heap is responsible for garbage collection (GC), the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer used data. However, the disadvantage is that the access speed is slower due to the dynamic allocation of memory at run time .
Stack (stack) (FILO):
Stack accesses faster than the heap, second only to registers. Data in the stack can be shared. The base data type and object handle are mainly stored in the stack. The disadvantage is that the data size and life cycle in the stack are deterministic and inflexible.
The biggest feature of the stack is data sharing. That is, what is often referred to as " there is a point, none is created ". It is important to note that the data share of the stack is different from the data share of the object reference to the same object, and there is no effect between the sharing of the previous stack, that is, modifying the value of one variable does not affect the value of the other variable. The object's cause is affected by other objects. The stack mainly contains some basic types of variables (, int, short, long, byte, float, double, Boolean, char) and object handle.
As can be seen, garbage collection GC is for heap heap, and stack because itself is filo (first in the last out). Advanced after out, can be automatically released. So that you can see that the new one was created and put into the heap heap!
The Heap is also divided into young, old, permanet.
Java Heap and Stack