Heap memory

Heap memory is divided into primarily two generations: Young Generation and Old Generation. There is a PERM GENERATION that is a part of heap memory until Java 7, while from Java 8, the PERM GENERATION is replaced by METASPACE. METASPACE is not part of the heap memory but is part of the Native Memory. Set size of METASPACE using the -XX:MaxMetaspaceSize option. It is critical to consider this setting when going to production since if METASPACE takes up excessive memory, it affects the application's performance:

Java 8 memory management

The Young Generation is where objects are created and allocated; it's for young objects. The Young Generation is further divided into Survivor Space. The following is the Hotspot Heap Structure:

The eden area is, by default, bigger than Survivor Space. All the objects are created first in the eden area. When eden is full, minor GC is triggered, which will quickly scan the object's references, and unreferenced objects are marked dead and collected. The Survivor Space area in either of them would always be empty. Objects that survived in eden during minor GC will be moved to the empty Survivor Space. We might wonder why there are two Survivor Space areas and not one. The reason is to avoid memory fragmentation. When the Young Generation runs through and removes dead objects from the Survivor Space, it leaves holes in the memory and needs compaction. To avoid compaction, JVM moves surviving objects from one Survivor Space to another. This ping-pong of live objects from eden and one Survivor Space to another happens until the following conditions occur:

  • Objects reach maximum tenuring threshold. This means objects are no longer young.
  • Survivor Space is full and cannot accommodate any new objects.

When the preceding conditions happen, objects are moved to the Old Generation

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset