JVM memory management ------ GC algorithm final solution (copying algorithms and tagging/sorting algorithms)

Source: Internet
Author: User

The replication algorithm divides the memory into two intervals. at any point in time, all dynamically allocated objects can only be allocated within one interval (called between activity zones ), the other interval (called the idle interval) is idle..

Next, the GC thread will copy all the surviving objects in the active zone to the idle zone and sort them in strict order according to the memory address. At the same time, the GC thread points the memory reference address of the updated surviving object to the new memory address..

The replication algorithm makes up for the memory layout disorder in the tag/clear algorithm.. But at the same time, its shortcomings are quite obvious.

It wastes half of the memory.This is terrible.

The time it takes to copy this job will become negligible when the object survival rate reaches a certain level..

To use the replication algorithm, the survival rate of objects must be very low at least, and the most important thing is that we must overcome the waste of 50% of memory..

Mark and organize. The following LZ will introduce what has been done in these two phases.

Tag: the first phase of the tag is exactly the same as the tag/clear algorithm. it traverses the GC Roots and then marks the surviving objects.

Sort: Move all the surviving objects, sort them in order of memory addresses, and recycle all the memory after the end memory address. Therefore, the second stage is called the sorting stage.

The diagrams before and after GC are very similar to those of the replication algorithm, but there is no difference between the active and idle intervals, and the process is very similar to that of the mark/clear algorithm, let's take a look at the status and layout of objects in memory before GC, as shown in.

The tag/sort algorithm can not only make up for the shortcomings of the tag/clear algorithm, but also eliminate the high cost of halving the memory in the replication algorithm.It can be said to be two birds, one stone, two birds, one stone .... One female and two male?

The only drawback of the tagging/sorting algorithm is its low efficiency.Not only to mark all surviving objects, but also to sort out the reference addresses of all surviving objects. In terms of efficiency, the tag/sorting algorithm is lower than the copy algorithm.

Algorithm Summary

1. All three algorithms are based on the root search algorithm to determine whether an object should be recycled. The theoretical basis for supporting root search algorithms to work normally is the scope of variables in the syntax. Therefore, the most fundamental way to prevent memory leakage is to master the variable scope, instead of using the C/C ++ memory management method mentioned in the previous memory management miscellaneous.

2. When the GC thread is enabled or the GC process starts, they must stop the world ).

Efficiency: Copy algorithm> tag/sort algorithm> tag/clear algorithm (the efficiency here is only a simple comparison of time complexity, which is not necessarily the case in reality ).

Memory Uniformity: Copy algorithm = markup/Sorting Algorithm> markup/clearing algorithm.

Memory usage:Tag/sort algorithm =Tag/clear Algorithm> Copy an algorithm.

No best algorithm, only the most suitable one.

Generational collection Algorithm.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.