Memory in Java is divided into two types, heap memory and stack memory
One, stack memory
The basic type variable and the object reference variable defined in the function, Java allocates the stack memory to it. Once you leave the scope, the memory will be freed.
Java also allocates stack space to variables defined in the code block within the function.
Second, heap memory
The memory created with the New keyword in Java is allocated on the heap, which is automatically managed by the Java Virtual machine's garbage collector. In the stack, you can define special reference variables to point to the objects allocated in the heap memory or to the first address of the array, and use these reference variables to manipulate the heap memory. When there is no reference to the memory in these heaps, the memory becomes inaccessible memory, which becomes garbage, but is not reclaimed at this time. GC garbage collector, which recycles the garbage at an indeterminate time.
[1] http://blog.csdn.net/huozhicheng/article/details/6575960
[Java] Heap and stack differences