Objects are allocated in Eden first

In most cases, objects are allocated in the Eden region of the young generation. A Minor GC occurs when the Eden area does not have enough space to allocate. We can turn on the output of GC logs with the following parameters:

  • ** -xx :+PrintGC ** Prints GC logs
  • ** -xx :+PrintGCDetails ** Prints the GC details log
  • ** -xx :+PrintGCTimeStamps ** Prints GC timestamps (in base time)
  • ** -xx :+PrintGCDateStamps ** Prints the GC timestamp (in the form of a date, e.g. 2013-05-04T21:53:59.234+0800)

Big object goes straight to the old age

Large objects are Java objects that require a large amount of contiguous memory space, such as long strings or byte[] arrays. The JVM provides a * * – XX: PretenureSizeThreshold * * parameter, make greater than the value of the object directly into old age, avoid between Eden area and Survivor area a lot of memory copy.

A variety of GC

For HotSpot VIRTUAL machine implementations, GC can be broadly divided into two categories:

  • Partial GC: Collects THE GC from a part of the heap
    • Minor GC (Young GC) : GC that occurs in the Young generation.
    • Mixed GC: GC specific to the G1 collector that collects the entire young generation as well as parts of the old generation.
  • Full GC: Global GC for the entire new generation, old generation, metaspace (java8 + replaces Perm Gen), Full GC is typically 10 times slower than Minor GC.

Recovery strategy

In the young generation, there are From Survivor (S0) and To Survivor (S1) zones in addition To Eden zone. After a Minor GC, the JVM copies the surviving objects into the Survivor region (one of S0 and S1) using a copy algorithm. At this point, the age of the objects in the Survivor zone is marked as 1, and each time they survive a Minor GC in the Survivor zone, their age is +1, and when they reach a certain threshold (15 by default), they are promoted to the old age. The threshold for promotion to the old age can be set with the parameter -xx :MaxTenuringThreshold. The JVM does not always require an object’s age to reach MaxTenuringThreshold in order to advance to the old age. If the sum of all object sizes of the same age in the Survivor space is greater than half of the Survivor space, objects older than or equal to this age can enter the old age directly.