Introduction to generational models

Depending on how you write code, objects are created and used in different ways, and they actually have different lifetimes, so the JVM divides Java heap memory into two regions: young generation and old generation

Take a look at the method area, Java virtual machine stack, and Java heap memory diagram in the code below

public class HelloWorld {
    private static Demo1 demo1 = new Demo1();

    public static void main(String[] args) throws InterruptedException {
        executeDemo2();
        while (true) {
            executeDemo1();
            Thread.sleep(6000); }}public static void executeDemo2(a) {
        Demo2 demo2 = new Demo2();
        demo2.execute();
    }

    public static void executeDemo1(a) { demo1.execute(); }}Copy the code

A static variable demo1 in the HelloWorld class references the demo1 object. Since static variables are used in memory for a long time, the demo1 object persists in the young generation for a while, and then finally enters the old age.

ExecuteDemo2 () will be called. Execute () of Demo2 will be executed. ExecuteDemo2 will create a Demo2 object, which will be recycled as soon as it is used, so it will be placed in the young generation and referenced by local variables in the stack frame

Once the executeDemo2() method has completed execution, the stack frame of the method is removed from the stack, and the stack memory is immediately reclaimed. Demo2 objects of the corresponding age are referred to as garbage objects and are waiting for garbage collection

This is followed by the while loop, which periodically calls the executeDemo1() method

Garbage collection mechanism algorithm

Replication algorithm

The new generation of garbage collection algorithms is called replication algorithms, which, as the name implies, copy inventory objects to another location.

This is called a “replication algorithm”, which divides the new generation of memory into two memory regions and then uses only one of them

When that area of memory is nearly full, the surviving objects in it are moved to another area of memory at once, ensuring that there is no memory fragmentation

The garbage objects in the original memory area are then collected once, freeing another memory area. Two areas of memory are used repeatedly

  • Disadvantages of replication algorithm

According to the above idea, it can be seen from the figure that the general memory needs to be left unused every time, which is a waste of memory. Add 1G memory space to the new generation, but according to the above model, only 512MB space can be used, so the memory utilization rate is only 50%

Constantly create objects and allocation in our code in Cenozoic, but generally there is no reference, objects created by soon become garbage, recycling garbage collection mechanism, at this time most of the object are live cycle is very short, may be created after 1 millisecond nobody quotes, so after every new generation garbage collection, Most objects are garbage collected, and only a few survive.

  • The solution

Therefore, in the JVM memory model, the new generation memory region is divided into three parts:

One Eden area and two Survivor areas. Eden area accounts for 80% of the memory space, and each Survivor area accounts for 10% of the memory space. For example, if Eden area has 800MB of memory, each Survivor area has 100MB, as shown below:

The Eden zone and one of the Survivor zones can be used normally, which means 900MB of memory can be used

At the beginning, all objects are allocated in Eden area. If Eden area is nearly full, garbage collection will be triggered, and all surviving objects in Eden area will be transferred to an empty Survivor area at a time. Then the Eden region will be cleared and new objects will be allocated to the Eden region. In this case, the Eden region and a Survivor region have objects that have survived since the last Minor GC.

If Eden is full the next time, the Minor GC is triggered again, and the Eden GC and surviving objects from the Survivor zone containing the surviving objects from the last Minor GC are transferred to another Survivor zone

The best thing about this is that only 10% of the memory is free, and 90% of the memory is used

Garbage collection performance, memory fragmentation control, and efficiency in memory usage are all very good

Memory marking algorithm

Garbage is used in the area of memory object tag, mark which objects can be garbage collected, and then directly to garbage collection of objects in the memory area, the memory available, in the area used by the memory, recycling away a lot of garbage objects, but retains some quoted the survival of the object. However, the surviving objects are scattered all over the memory area, which is very messy and causes a large amount of memory fragmentation

Some memory is too small to be used by other objects, resulting in memory waste

The young generation

  • Most normal objects are allocated first in the new generation

The old s

Under what circumstances will objects in the new generation enter the old age?

  • Enter the old age after avoiding 15 GC

    If an instance object in the new generation has not been collected after 15 successful garbage collections, it will be moved to the old generation

    This can be set with the JVM parameter “-xx :MaxTenuringThreshold”, which is 15 by default

  • Dynamic object age determination

    This rule lets an object not wait 15 GC before it reaches the old age

    If the total size of a group of objects in the Survivor region of the current object is greater than 50% of the size of the Survivor region’s memory, then the group of objects greater than or equal to the age of the group can enter the old age

  • Big object goes straight to the old age

    JVM parameters: is “- XX: PretenureSizeThreshold”, can put his value is set to bytes, such as “1048576” byte, is 1 MB, if you create a greater than this parameter is set the size of the object, the big object directly into old age, not after a new generation

  • There are too many objects after Minor GC to put into Survivor zone

    What if after the Minor GC there are too many surviving objects left to fit into another Survivor zone

After GC, there are still 150MB objects in the Edenl region and 100MB in the Survivor region, so they cannot be placed in the S region and are moved directly to the old age

Old chronospatial allocation guarantee rules

There is a problem: if a large number of objects survive in the new generation, which really does not fit in the Survivor zone, and must be moved to the old generation, but if there is not enough space in the old generation for these objects, what does the JVM do?

First, before performing any Minor GC, the JVM checks to see if the amount of free memory available in the old generation is greater than the total size of all objects in the new generation.

** Why check this? ** Because in the most extreme case, it’s possible that after the Minicar Minor GC, all objects in the minicar will survive, and then all objects in the minicar will go into the old age.

If you find that the memory size of the old generation is larger than that of all the new generation objects, you can do a MinorGC on the new generation, because even if all the objects survive after the MinorGC and the Survivor zone is no longer available, you can still migrate to the old generation.

But let’s say that before performing the Minor GC, the available memory of the old generation is less than the total object size of the new generation

Is it possible that all the new generation objects survive after the Minor GC and then all need to be moved to the old age, but there isn’t enough space in the old age?

So if, before Minor GC, you find that the available memory of the old generation is less than the entire object size of the new generation, you look at “-xx :-“

**HandlePromotionFailure ** whether the parameter is set

The next step is to see if the memory size of the old age is larger than the average size of objects entering the old age after each Minor GC

For example, after each Minor GC, on average, around 10MB of objects went into the old age, so the available memory of the old age was greater than 10MB.

This means that it is likely that objects around 10MB will also go into the old age after this Minor GC, when the old age space is sufficient

If the previous step fails, or if the “-xx: -handlePromotionFailure” parameter is not set, then a “Full GC” is triggered, which is a garbage collection of the old years to free up as much memory as possible, and then a Minor GC is performed

If the previous two steps are judged successful, then it’s time to take a risk and try the Minor GC. There are several possibilities for Minor GC at this point

  • The first possibility is that after the Minor GC, the remaining surviving objects are smaller than the size of the Survivor zone, and then the surviving objects enter the Survivor zone.

  • The second possibility is that after the Minor GC, the remaining surviving objects are larger than the Survivor region, but smaller than the available memory of the old age, and you just go to the old age.

  • The third possibility is that, unfortunately, after the Minor GC, the size of the remaining surviving objects is greater than the size of the Survivor region and the size of the memory available in the old years. A “Handle Promotion Failure” occurs at which point a “Full GC” is triggered.

Full GC does garbage collection for the older generation, but also generally for the new generation.

At this point, unreferenced objects from the old age must be reclaimed before any surviving objects left after the Minor GC can be recycled into the old age.

If after Full GC, there is still not enough space in the old generation to store the remaining surviving objects after Minor GC, then this will result in what is called

The OOM memory overflows

The above text description is rather convoluted, which can be viewed by referring to the following flow chart:

Old age garbage collection algorithm

The timing of the garbage collection trigger

  • Before the Minor GC, a check was made to find that there are probably too many objects that need to go to the old age after the Minor GC

    The Full GC is triggered early and then the Minor GC is carried through

  • After the Minor GC, there are too many remaining objects to fit into the old age

In the old days, we used the tag sorting algorithm

If the old Full GC garbage collection frequently occurs in the system, the system performance will be seriously affected, and the situation of frequent lag occurs