Working with Generations of Objects

Java garbage collectors implement a generational garbage collection strategy that can classify objects by age.

Such a need (to mark and compact all objects) in the JVM can be called inefficient. Since as a large number of objects are allocated, their list grows, which leads to an increase in garbage collection time. Empirical analysis of applications has shown that most objects in Java are short-lived.

The heap memory area in the JVM is divided into three sections:

Working with Generations of Objects

Younger generation

Newly created objects start in the younger generation. The younger generation is further subdivided into two categories.

  • Eden Space - All new objects start here, they are allocated initial memory.
  • Survivor Spaces (FromSpace and ToSpace) - Objects are moved here from Eden after surviving one garbage collection cycle.

The process when objects are garbage collected from the younger generation is called a minor garbage collection event.

When Eden's space is filled with objects, a small garbage collection is performed. All dead objects are removed, and all living ones are moved to one of the remaining two spaces. The small GC also checks objects in survivor space and moves them to another (next) survivor space.

Let's take the following sequence as an example.

  1. There are objects of both types (living and dead) in Eden.
  2. A small GC occurs - all dead objects are removed from Eden. All living objects are moved to space-1 (FromSpace). Eden and space-2 are now empty.
  3. New objects are created and added to Eden. Some objects in Eden and space-1 become dead.
  4. A small GC occurs - all dead objects are removed from Eden and space-1. All living objects are moved to space-2 (ToSpace). Eden and space-1 are empty.

Thus, at any time, one of the survivor spaces is always empty. When survivors reach a certain threshold for moving through survivor spaces, they advance to an older generation.

You can use the -Xmn flag to set the young generation size .

Older generation

Objects that live a significant amount of time (for example, most of the lifetime of a program) eventually become older objects - centenarians. It is also known as the regular generation and contains objects that have been left in Survivor Spaces for a long time.

An object's lifetime threshold determines how many garbage collection cycles it must go through before it is moved to the older generation. The process when objects are sent to the garbage from the older generation is called the main garbage collection event.

You can use the -Xms and -Xmx flags to set the initial and maximum heap memory size .

Because Java uses generational garbage collection, the more garbage collection events an object experiences, the further it moves on the heap. He starts in the younger generation and eventually ends up in the regular generation if he lives long enough.

To understand the promotion of objects between spaces and generations, consider the following example:

When an object is created, it is first placed in the Eden space of the young generation.

As soon as a small garbage collection occurs, living objects from Eden are moved to FromSpace. When the next minor garbage collection occurs, living objects from both Eden and space are moved to ToSpace.

This cycle continues a certain number of times. If the object is still "in service" after this point, the next garbage collection cycle will move it to older generation space.

Permanent generation and metaspace

Metadata like classes and methods are stored in persistent generation. The JVM populates it at runtime based on the classes used by the application. Classes that are no longer used can go from permanent generation to garbage.

You can use the -XX:PermGen and -XX:MaxPermGen flags to set the initial and maximum size of the permanent generation .

meta space

Since Java 8, the PermGen space has been replaced by the MetaSpace memory space. The implementation differs from PermGen - this heap space is now changed automatically.

This avoids the application's out-of-memory problem that occurs due to the limited size of PermGen's heap space. Metaspace memory can be garbage collected, and classes that are no longer in use will be automatically cleaned up when the metaspace reaches its maximum size.