This is the fourth day of my participation in Gwen Challenge

background

Today I saw an article explaining the generational model of JVM memory, which is relatively easy to understand. Here are some key points from the first article.

Generational model

In Java, objects created and used in different encoding methods have different life cycles and storage areas. The generation model is as follows:

  • The young generation
  • The old s
  • The permanent generation

The young generation

The young generation is used to store objects that will be recycled immediately after they are created and used, that is, objects with a short lifetime.

public class Test {
    public static void main(String[] args) {
        while(true) { func(); }}private static void func(a) {
        Manager manager = newManager(); manager.compute(); }}Copy the code

The manager object in the above code is a short-lived object that is created every time the func() method is executed, then its compute() method is executed, and then, perhaps a millisecond later, garbage collected.

The manager object will be placed in the young generation.

The old s

Older generations are used to store objects that need to live long after they are created.

public class Test {
    private static Manager manager = new Manager();

    public static void main(String[] args) {
        while(true) { func(); }}private static void func(a) { manager.compute(); }}Copy the code

The above code defines a class static variable manager, which is initially stored in the young generation and then in the old generation

The manager instance object is not garbage collected because it is always referenced by Kafka static variables and then resides in Java heap memory.

Why is it divided into young generation and old generation?

The root cause is that objects with different lifecycles have different garbage collection mechanisms, so there needs to be two zones for different objects:

  • For objects in the younger generation, they are typically recycled very quickly after being created, so a garbage collection algorithm is needed
  • For objects in the old age, they are characterized by long-term existence, so another garbage collection algorithm is required

The permanent generation

A permanent generation in the JVM is actually a method area, and you can think of a permanent generation as a place for class information.