Java interview jvm garbage collection mechanism

In my fourth year of work, for various reasons (low salary and low salary...) I had to leave the company where I worked for nearly three years and embarked on the journey of interviewing, this time the interview was the same as three years ago The interview has changed a lot, and every interview will mention the garbage collection mechanism of jvm!

 

The memory structure of jvm is divided into heap stack and method area

 

Let's talk about the garbage collection algorithm of jvm's heap heap

The jvm divides the heap memory into the new generation and the old generation (that is, the so-called striped garbage collection), and is divided into eden and survivor1 and survivor2 in the heap new generation

Garbage collection algorithms are divided into:

1. Mark-sweep algorithm;

2. Mark-copy algorithm;

3. Marking-collating algorithm;

 

Banded garbage collection mechanism:

Cenozoic:

When the object is created, Object obj = new Object(); The reference of the obj object will be stored in the method area of ​​the early method area, and the real object will be stored in the heap heap eden . When the jvm generates gc under necessary conditions, the jvm will According to the reference counter of the object or the reachability of the object, use the mark-copy algorithm (for the new generation of the heap heap) to mark the surviving objects in eden, age +1, and then copy the marked surviving objects to the heap heap The survivor1 clears the eden, and when the GC occurs in the survivor1, the surviving objects are marked and copied to the survivor2 and then the survivor1 is emptied, alternately, there are many objects in the new generation, and the older surviving objects will be placed in the old generation. 

Old age:

When GC occurs in the old generation jvm of the heap heap, the mark sorting algorithm will be used to mark the objects that survive on the heap, and then the objects will be sorted to avoid storage fragmentation, resulting in large objects not being able to apply for memory.

 

Different garbage collection algorithms are used in the new generation and the old generation, because the objects in the new generation will basically die after they are created, and the survival rate is very low. Therefore, the mark copy algorithm is used to copy a small number of remaining objects to another block. place, and then empty the storage area;

In the old age, most of the objects are still alive, so use the mark sorting algorithm to clear the dead objects, then sort out the storage space, and release the continuous heap heap space address, which is more conducive to large and old ones. object request memory

 

 

The above is a personal understanding, and also hope to advise!

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326332205&siteId=291194637