Performance optimization|During the interview process, you will be more divided if you learn about JVM

Performance optimization|Garbage collection algorithm

Mark-clear algorithm

Marking-clearing is divided into two stages: marking and clearing. In the marking phase, the jvm will mark the objects that need to be recycled. After the marking phase is completed, the jvm starts to perform the cleanup action. This stage will clear those marked objects that need to be recycled .
Comparison before and after memory sorting:
black is a living object, gray is a garbage object
Insert picture description here
Insert picture description here

Conclusion: Using the mark-sweep algorithm, after cleaning up the garbage, you will find that the distribution of surviving objects is relatively scattered. If there are large objects to be allocated, it is difficult to have continuous space for allocation;
disadvantages: low efficiency, space fragmentation

Copy algorithm

In order to solve the problem of memory fragmentation, JVM masters have developed a copy algorithm. The principle of the copy algorithm is to divide the memory space into two. When one of the memory is used up, the surviving object will be copied to the other memory. The memory block is directly cleaned up, so that the problem of memory fragmentation will not occur.
Use copy algorithm, memory before and after comparison
Insert picture description here
Insert picture description here

Conclusion: The problem of memory fragmentation is solved, but it will reduce the memory space by half, which is suitable for areas with few surviving objects.

Tag sorting algorithm

The steps of the mark arranging algorithm are the same as mark-clear, but the final step is arranging, which is used to arrange the memory fragments caused by surviving objects. Use mark-arrange to compare the memory before and after:

Insert picture description here
Insert picture description here

Conclusion: It can be seen that there is no memory fragmentation after marking-defragmentation, and all surviving objects are moved aside.

Generational collection algorithm

The generational collection algorithm mainly divides the memory into two generations, one is the young generation and the other is the old generation. The copy algorithm is used in the young generation. Because the young generation has fewer objects, it is more suitable to use the copy algorithm, and the old generation uses tags. Sorting algorithm, because there is less garbage in the old age, it is suitable for marking sorting algorithm

Wechat search for a search [Le Zai open talk] Follow the handsome me, reply [Receive dry goods], there will be a lot of interview materials and architect must-read books waiting for you to choose, including java basics, java concurrency, microservices, middleware, etc. More information is waiting for you.

Guess you like

Origin blog.csdn.net/weixin_34311210/article/details/109447191