Java Virtual machine garbage collection algorithm principle

Source: Internet
Author: User

In addition to releasing objects that are no longer referenced, the garbage collector handles heap fragments. The new object is allocated space and the object that is no longer referenced is freed, so the heap memory is free from the active object. Requests to allocate new objects may have to increase the size of the heap space, although the total amount of free space that can be used is sufficient. This is because there is no contiguous free space in the heap to place new objects.

Garbage collector algorithm

  Any garbage collection algorithm must do two things, first of all, it must detect the garbage object. Second, it must reclaim the heap space used by the garbage object and return it to the program. From the root object, any object that can be touched is considered an "active" object (this object is accessible if a running program can access a reference path between the root object and an object)

(1) Reference count collector

is an early strategy for garbage collection, in which each object in the heap has a counter. When an object is created and a reference to that object is assigned to a variable, the object's reference counter is set to 1.

When any other variable is assigned a reference to the object, the count is added to 1.

When a reference to an object exceeds the lifetime or a new value is set, the object's reference count is reduced by 1.

Any object that references a counter of 0 can be garbage collected.

Advantage: The reference counter can be interwoven into the program, which is advantageous for the real-time environment where the program cannot be interrupted for a long time. Cons: Reference counting cannot detect loops (i.e. two or more objects referencing each other)

(2) Tracking collector

An object reference graph that starts at the root node, and the objects encountered during the trace are tagged in some way. Either set the tag on the object itself or use a separate bitmap to set the tag. When the trace is finished, unmarked objects know that they are inaccessible and can be garbage collected.

The Java Virtual machine's garbage collector may have a strategy for dealing with heap fragments. The two strategies that are typically used to mark and clear collectors are compression and copying. The principle of implementation is to move objects quickly to reduce heap fragments. The compression collector moves the active object across the idle area to the other end of the heap, and a large contiguous idle area appears at the other end of the heap. References to all moved objects are also updated to point to the new location. (a reference to an object actually points to a table of object handles.) The object handle points to the actual location of the object in the heap. When the object is moved, only the handle needs to be updated to the new location. The reference to the object in all the programs still points to the handle with the new value, and the handle itself is not moved.

Pros: Simplifies the effort to eliminate heap fragments. Cons: Every access to the object brings a loss of performance.

(3) Copy Collector

Move all the active objects to a new area. In the process of copying, they are arranged next to each other, eliminating their voids in the old area. The object is quickly copied to a new area, while the steering pointer remains in its original position. The steering pointer allows the garbage collector to discover references to objects that have been transferred. The garbage collector can then set these references to the values of the steering pointer, so they now point to the new position of the object. The feature of this algorithm is "Stop and copy". Only one area is used at any time, and the object is allocated in the same area until the area is exhausted. At this point, the execution of the program is terminated, the heap is traversed, and the active object encountered during the traversal is copied to another region. When the stop and copy process is finished, the program resumes execution.

Advantage: objects can be copied from the root object as they are discovered, and there is no longer a distinction between marking and purging. Cons: To allocate two times the heap memory, each time a long life cycle of objects copied back and forth, consuming a lot of time.

(4) collector collected by generation

The problem of inefficient copy collectors is solved by grouping the lifetime of objects. In this method, the heap is divided into two or more sub-heaps, and each sub-heap serves as a "generation" object. The youngest generation has the most frequent garbage collection. If one of the youngest objects has survived several garbage collections, the object has grown into the highest-life generation. be transferred to another sub-heap.

(5) Adaptive Collector

The adaptive algorithm monitors the situation in the heap and adjusts accordingly to the appropriate garbage collection technology.

The above garbage collection mechanism will cause the program interruption-running process, and does not apply to the high-real-time system.

Reference: "Deep Java Virtual machine"

Java Virtual machine garbage collection algorithm principle

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.