Garbage collection

To understand memory optimization, it is a good idea to understand how memory management works in Java. Objects reside in heap in Java. Heap is created when JVM starts, and it can resize itself when needed (based on the minimum and maximum size, that is, -Xms and -Xmx, respectively, assigned in the configuration).

Heap is divided into two spaces or generations: young space and old space, as shown in the following figure:

The young space is reserved for the allocation of new objects. The young space consists of an area called Eden and two smaller survivor spaces. When the young space becomes full, garbage is collected by running a special process called young collection, where all the objects that have lived long enough are promoted to the old space. When the old space becomes full, the garbage is collected there by running a process called old collection.

The logic behind nursery/young space is that most objects have a very short lifespan. A young collection is designed to be fast at finding newly allocated objects and moving them to the old space.

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

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