Legend in ancient times there was a four-horn four-footed monster: named Xi. Because of the winter snow led to the evening did not eat things, so the evening often to the nearby village to find food, because of its huge body, temper, fierce abnormal, brought great disaster to the villagers.

Later, there was a clever child, named Nian, who taught us how to get rid of the “Xi” : with firecrackers, light to drive it away, heavy to hurt it. Every year December 30, xi will come to the village, the village is keeping the night, set off firecrackers away xi. Hence New Year’s Eve.

Let’s think of Xi as a Java program that constantly eats up the machine’s memory. Let’s call it nian. The person in charge of Java virtual machine memory is Nian, let’s call him Brother Nian.

Niango’s territory

Nian ge managed the site is mainly divided into five areas: heap, method area, virtual machine stack, local method stack, program counter. You can think of the threads as villagers and the heap as a shared area for villagers.

As shown below:

The heap can be subdivided into the new generation and the old generation. The ratio between the new generation and the old generation is 1:2, while the new generation can be subdivided into Eden zone and two Survivor zones. The size of Eden zone and Survivor zone is 8:1.

As shown in the picture below, Nian and the villagers share the heap memory, while Nian, the administrator, manages the heap memory. The numbers 1, 8, and 20 indicate the number of memory blocks occupied.

Nian’s appetite

Nian beast’s appetite is hundreds of times that of the villagers, nian beast disguised as a village escaped the administrator nian Elder brother’s inspection, nian elder brother for this big stomach are directly allocated to the old age, because the big stomach king needs continuous memory for it to eat, and the new generation of fragments more do not meet the conditions. In the Java world, the most typical eater is a large object: a very long string, for example, or an array with a large number of elements.

As shown in the picture below, the villagers were allocated to the new generation to eat the memory, while the Nian beast was directly allocated to the old age.

Large numbers of nien invaded

After tasting the sweet taste, Nian beast began to call its relatives and friends constantly. A large number of Nian beasts were assigned to the old era, which directly led to the lack of memory space in the old Era, as shown in the figure below:

Code demo

We use code to demonstrate next year’s beast invasion:

  • Created 3 year beasts, each taking up 10 MB of memory.
public class SpringFestivalOOM {
    public static void main(String[] args) {
        // Year beast 1/2/3, all occupy 10 MB memory
        byte[] nianShou1 = new byte[10 * 1024 * 1024];
        byte[] nianShou2 = new byte[10 * 1024 * 1024];
        byte[] nianShou3 = new byte[10 * 1024 * 1024]; }}Copy the code
  • Compile this program.
javac SpringFestivalOOM.java
Copy the code
  • Run this program and set the maximum heap memory to 20 MB.
java -Xms20M -Xmx20M SpringFestivalOOM
Copy the code

Because 30 MB of memory is larger than the maximum heap size of 20 MB, a heap overflow exception is thrown, as shown in the following figure:

At this time niangge and the villagers found that there were so many years of animals occupied our territory, quickly eliminate them!

Chased away nian

The villagers got together with Nian To discuss how to solve the problem. The reason was that there were too many Nian animals and they needed to reduce the number of friends they called to eat the memory.

In our Java world, we reduce the frequent creation of large objects.

Our programmers often appear after the local write the code will be a problem, to online after went out of the question, probably the reason is that the online environment data, it is easy to appear frequently create large objects, such as large-scale promotional activities, in a short period of time you need to create a large number of orders, and order data is more complex, there are a lot of fields, could take up a lot of memory space, As a result, garbage collection is triggered frequently, and garbage collection times are stopped the world, and application performance is reduced.

Shou sui

On the eve of New Year’s Eve, there is a “shou Sui”, where villagers gather to have a meal and wait for the bell to ring. Wait until dawn to visit relatives and neighbors.

The shousui process can only be done at home, so it can be regarded as garbage collection, other threads can not work, which is the origin of Stop the world.

As is shown in the picture below, before the New Year’s Eve, villagers can go to other places for activities. On the Eve of the New Year’s Eve, they can only stay at home and stay up late.

conclusion

This article through the New Year’s Eve story to explain the garbage collection mechanism in Java, because the story is relatively simple, so there is no in-depth explanation of the garbage collection algorithm, this article can only be counted as the introduction to garbage collection, I hope to give you some inspiration, students who are familiar with the JVM should learn the origin of the New Year’s Eve

  • Villagers use heap areas as small objectsThe new generationYear beast uses heap area directly as large objectThe old s.
  • On New Year’s Eve, a lot ofNien invasionOld age, lead toInsufficient heap area memoryTo triggerThe garbage collectionMechanism.
  • Shousui means staying at home to celebrate the New Year, and when the garbage is collected,It stops other threadsStop the world.
  • Avoid codeFrequently copy or createBig objects are something you have to do to avoid problems when you go live.
  • New Year’s Eve also representsFor oldIsn’t that what garbage collection is all about?

Happy New Year’s Eve, happy New Year, 2021 come true, good things in pairs ~