jvm--tag-Clear algorithm Mark-sweep

Source: Internet
Author: User

Objective

The advent of automatic garbage collection makes programming more simple, so that we do not need to consider the memory allocation and release problems, but more focus on the implementation of our product features. But we still need to take the time to understand how the garbage collection mechanism works, so that we can better perform the performance tuning of our application later.

There are currently four basic garbage collection algorithms, the tag-purge algorithm (mark-sweep), the tag-compression algorithm (mark-compact), the copy algorithm (copying), and the reference counting algorithm (reference counting). The modern garbage collection algorithm is generally composed of these four kinds of algorithms, for example, the heap (heap) is a part of the tag-clearing algorithm, the heap (heap) of another part of the use of replication algorithm and so on. Today we mainly look at the principle of the tag-purge algorithm.

Basic concepts

Before we understand the tag-clear algorithm, we need to understand a few basic concepts.

The first is mutator and collector, these two nouns often appear in the garbage collection algorithm, collector refers to the garbage collector, and mutator refers to the other than the garbage collector, such as our application itself. Mutator's duties are generally new (allocating memory), read (reading from memory), write (writing content to memory), and collector is reclaiming unused memory for mutator to use for new operations.

The second basic concept is about mutator roots (mutator root object), the Mutator root object generally refers to an object that is allocated outside of the heap memory and can directly be directly accessed by the mutator, generally referred to as static/global variables and thread-local variables ( In Java, variables stored in the Java.lang.ThreadLocal and variables allocated on the stack-the temporary variables inside the method, and so on-belong to this class.

The third basic concept is about the definition of the reachable object, from which the Mutator root object is traversed, and the objects that can be accessed are called reachable objects. These objects are also objects that mutator (your application) is using.

Algorithm principle

As the name implies, the tag-purge algorithm is divided into two stages, Mark (Mark) and Purge (sweep).

During the tagging phase, collector is traversed from the Mutator root object, identifying objects that can be accessed from the Mutator root object, typically in the header of the object, and recording it as reachable objects.

In the purge phase, collector traverses the heap memory (heap memories) from beginning to end, and if an object is found not to be marked as an object-by reading the header information of the object, it is recycled.

As we can see, in the Mark phase, from the root object 1 You can access to the B object, from the B object can be accessed to the E object, so the B,e object is reachable. Similarly, f,g,j,k are also accessible to the object. At the sweep stage, all non-accessible objects are collector recycled. At the same time,collector will pause the entire application during the marking and purging phase (mutator), waiting for the mark to clear until the end of the application is resumed, which is the origin of the word Stop-the-world.

Then we look at how the general garbage collection action is triggered, here is the pseudo-code for the new operation of Mutator:

New (): Ref <-Allocate ()///Allocate a newer memory to ref pointer if ref = = NULL collect ()//low memory, trigger garbage collection ref <-allocate ( If ref = = null throw "Out of memory"//garbage collection is still out of RAM, then throw out of a memory error return refatomic collect (): Markfromroots () Sweep (heapstart,heapend)

And here's the corresponding Mark algorithm:

Markfromroots ():     worklist <- empty    for each  fld in roots  //Traverse all mutator root objects         ref  <- *fld        if ref != null & & isnotmarked (ref)   //if it is accessible and not marked, mark the object directly and add it to the worklist             setmarked (ref)             add (Worklist,ref)            mark () mark ():     while not isempty (worklist)            ref <- remove (worklist)   //pops the last element of the worklist, assigns the value to ref           for each fld in pointers (ref)   // Iterates through all the pointer fields of a ref object, if its pointer field (child) is accessible, directly marks it asCan reach the object and add it to worklist           //in such a way to achieve deep traversal, Until all accessible objects below the object are marked as reachable objects.                 child  <- *fld                 if child != null && isnotmarked (Child)                     setmarked (Child)                      Add (Worklist,child)

At the end of the mark phase, the sweep algorithm is simpler, starting from the start of the heap memory, traversing all the objects linearly until the end of the heap memory, and if the object is an object (marked in the mark Stage), remove the tag bit directly (for the next mark), If the object is unreachable, free up memory directly.

Sweep (start,end): Scan <-start while scan < End If ismarked (scan) setunmarked (scan) Else Free (scan) scan <-nextobject (scan)

Disadvantages

The big drawback of the tag-purge algorithm is that garbage collection can cause a lot of memory fragmentation, as shown in the image above, there are three memory fragments in memory after garbage collection, assuming a square represents 1 units of memory, if an object needs to occupy 3 memory units, This causes the mutator to remain in a paused state, and collector has been trying to do garbage collection until out of Memory.

jvm--tag-Clear algorithm Mark-sweep

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.