The Java heap is the closest memory space for Java applications.
Almost all objects are present in the heap. Java heap completely self-active management, through the garbage collection mechanism, the garbage object will voluntarily clean up. No explicit release is required.
Depending on the Java garbage collection mechanism. the Java heap may have different structures.
The most common is to divide the entire Java heap into the new generation and the old age. Almost the same as humans. Aging is likely to find God, the new generation of objects or older objects, old age to store older objects. The Cenozoic may be divided into the Eden ,s0 and S1 areas, thes0 District and the s1 Zone is also known as the From-to area. They are two equally sized and interchangeable memory spaces for example , as seen in:
In most cases. The object is first assigned to the Eden area, and once again the new generation is recycled, assuming that the object still exists, it will enter s0 or s1, and then not after a cenozoic recovery, age +1. when the object age reaches a certain condition, it will be aging into the old age.
The following simple example shows the relationship between the Java Heap,theJava method area, and the Java stack. The code is as follows:
Package Cn.xhgg.test;public class Heaptest {private int id;public heaptest (int id) {this.id = ID;} public void Show () {SYSTEM.OUT.PRINTLN ("ID: " +id);} public static void Main (string[] args) {heaptest H1 = new heaptest (1); Heaptest h2 = new Heaptest (3); H1.show (); H2.show ();}}
Heaptest instance reference types are definitely allocated in the heap,H1,h2 Local variables are stored in the Java Stack, and point to two instances in the heap.
For example, as seen in:
Java Virtual Machine JVM Java Heap method area Java stack