In Java, heap memory is the largest portion of managed memory for the JVM and an important area for GC management.
Java heap memory is divided into two main regions:
1. Young generation. The young generation is divided into two zones, one is Eden zone and the other is Survivor zone. Survivor zone is divided into two parts, one is from zone and one is to zone.
2. The old days.
To be more specific, look at the picture:
First, the young generation
According to the professional research of IBM, nearly 98% of objects die overnight. Therefore, in most cases, objects will be allocated in Eden of the new generation. When there is not enough space in Eden, the virtual machine will trigger Minor GC.
The Minor GC is quick to collect. After passing the Minor GC, the Eden area is cleared. Most objects in Eden will be reclaimed at this time, and the remaining objects that do not need to be reclaimed will be placed in the Survivor FROM zone. If the FROM zone does not fit, they will be directly placed in the Old zone.
After the Minor GC is triggered again, objects in the Eden and FROM sectors are placed in the TO sector. Similarly, if you can’t fit into the To area, you can put into the Old area. The next time the Minor GC is triggered, objects in Eden and to will be placed in From. The Minor GC stores the young generation’s live data back and forth between the FROM and to sections.
Survival data in a Survivor zone is aged by 1 each time it undergoes a Minor GC, and the long-lived objects are moved to the old age when they reach the age of 15. Of course this 15, the JVM supports special Settings.
In addition, the virtual machine does not have to be 15 years old to be placed into the old age. If the size and size of objects of the same age in the Survivor space are more than half of that in the Survivor space, the objects whose age is about equal to that can be directly entered into the old age area without waiting for “adulthood”. This is similar to load balancing.
Two, the old age
Older generations occupy two-thirds of The heap and are only cleaned up during Major GC, which triggers “stop-the-world”. The larger the memory, the longer the STW, so bigger memory is not always better.
In addition to data from the young generation moving into the old, there is a special case: large objects. A large object is an object with a large amount of contiguous memory space that will go straight into the old age no matter how short its life cycle. The goal is to avoid a lot of memory replication between the Eden zone and the two Survivor zones. So pay attention to these big objects.