Garbage Collection Mechanism

Source: Internet
Author: User
I. Why does the garbage collection mechanism be required?

The Java Virtual Machine heap stores all the objects (new) created by the running Java program, but there is no clear code to release them. Garbage collection is the process of automatically releasing objects that are no longer used by the program.

2. How does the Garbage Collector work?

(1) When an object is no longer referenced by a program, the heap space it uses can be recycled. During the release process, the garbage collector runs the finalizer method of the object to be released.

(2) process chunks.

Iii. Advantages and disadvantages of the garbage collection mechanism?

Advantages: improves user productivity; helps programs maintain integrity, and avoids Java Virtual Machine crash caused by the programmer releasing memory due to errors.

Disadvantage: increasing the program burden may affect the program performance, because the Java Virtual Machine must track which objects are referenced and dynamically terminate and release objects that are no longer in use, which is better than explicitly releasing memory, more CPU time is required; programmers lack control over the release of useless objects.

4. Garbage collection algorithms?

Any garbage collection algorithm must do two things: Check the garbage objects; recycle the heap space used by the garbage objects and return it to the program.

1. reference the counting collector (early ):

Each object in the heap has a reference count. When an object is created and a reference to the object is assigned to a variable, the reference count of the object is set to 1. When any other variable is assigned as a reference to this object, the count is increased by 1. When a reference of an object exceeds the lifecycle or is set as a new value, the reference count of the object is reduced by 1. Any object with 0 reference count can be collected as garbage. When an object is garbage collected, the number of objects it references is reduced by 1. Therefore, in this method, the garbage collection action of other objects may be caused after an object is collected by garbage collection.

Advantage: The reference counting collector can be executed quickly and intertwined in the program running. It is advantageous to the real-time environment where the program is not interrupted for a long time.

Disadvantage: Circular references cannot be detected (that is, two or more objects reference each other ). If the parent object has a reference to the child object, the child object references the parent object in turn. In this way, even if they cannot be reached by the root object of the execution program, their reference count will never be zero. In addition, each increase or decrease in the reference count brings additional overhead.

2. tracking collector (now, Mark and clear collectors):

Trace the object reference graph starting from the root node. Objects encountered during tracing are marked in some way. You can set the tag on the object or use an Independent Bitmap to set the tag. When tracing ends, unlabeled objects cannot be reached and can be recycled.

The basic tracing algorithm is "mark and clear". The cleanup step includes the end of the object.

  The tag and clear collector usually uses two policies to deal with chunks: compression and copy.Both methods move objects quickly to reduce chunks.

3. Compression COLLECTOR:

Slide the active object across the idle area to one end of the heap. In this process, a large continuous idle area appears at the other end of the heap. The reference of all moved objects is also updated, pointing to a new location.

4. Copy the collector (STOP and copy ):

Move all activity objects to a new region. During the copy process, they are arranged closely, so they can be removed from the original gaps in the old area.

The general copy collector algorithm is called "Stop and copy ". The heap is divided into two areas. At any time, it is only used in one of them until this area is exhausted, the program execution is aborted, the heap is traversed, and the duration is copied to another area. In this way, "Stop and copy" repeatedly ".

Advantage: objects can be copied as they are discovered during the traversal process starting from the root object. There is no distinction between mark and clear.

Disadvantage: the memory can only be used in half at any time. In addition, all active objects must be copied during each collection.

5. By-generation COLLECTOR:

To solve the waste efficiency of the simple copy collector, the object with a long life cycle is copied back and forth every time.

Groups objects by lifetime to collect more young objects that appear temporarily. The heap is divided into two or more sub-Heaps. Each sub-heap serves a generation of objects, and the youngest generation performs the most frequent garbage collection. Because most objects appear for a short time, if an object remains alive after several garbage collection times, the object will be transferred to the child heap of a generation with a higher lifetime.

Advantage: it is applied to the copy algorithm, marking and clearing the algorithm. Improve the performance of these basic garbage collection algorithms.

Virtual machines are divided into three generations:Young Generation, old generation and permanent generation.

Young generation:It is divided into three zones (One Eden zone and two vor zones ). Most objects are generated in the Eden area. When the Eden zone is full, the surviving objects will be copied to the primary vor zone (one of the two). When the primary vor zone is full, the surviving objects in this region will be copied to another region vor. When the region VOR is full, the surviving objects will be copied from the first region vor, it will be copied as "tenured )". Note that the two regions of the same vor are symmetric and irrelevant. Therefore, objects copied from Eden and copied from the previous vor may exist in the same region, only objects copied from the first vor region are copied to the old district. In addition, there is always a blank vor area. At the same time, according to the program requirements, the VOR area can be configured as multiple objects, which can increase the time for the object to exist in the young generation and reduce the possibility of being put into the old generation.

Elder Generation:Objects that are still alive after N garbage collection in the young generation will be put into the old generation. Therefore, it can be considered that objects with long lifecycles are stored in the old generation.

Permanent generation:Used to store static files. Currently, Java classes and methods are supported. Persistent generation has no significant impact on garbage collection, but some applications may dynamically generate or call some classes, such as hibernate.

Generation-based garbage collection. Under what circumstances will garbage collection be triggered?

Because the object is divided into generations, the garbage collection area and time are different. There are two types of GC:

Scavenge GC: Generally, when a new object is generated and the Eden application space fails, scavenge GC is triggered to perform GC on the Eden area to clear non-surviving objects, and move the surviving objects to the same vor area. Then, sort out the two zones in the same vor. In this way, GC is performed on the Eden area of the young generation and will not affect the old generation. Because most objects start from the Eden area and the Eden area is not allocated much, GC in the Eden area is performed frequently. Because, it is generally necessary to use a fast and efficient algorithm here, so that Eden can be idle as soon as possible.

Full GC: sorts the entire heap, including young, tenured, and perm. Full GC is slower than scavenge GC because it is necessary to recycle the entire GC, so the number of full GC should be minimized. In the process of JVM optimization, a major part of the work is to adjust fullgc. Full GC may be caused by the following reasons: tenured is full; perm is full; system. GC () is displayed for calling.

V. Finalize ()

The finalize () method must be executed before the Garbage Collector releases an object, and the Finalize method of an object is only executed once.

(1) The Garbage Collector detects objects that are no longer referenced (the first scan );

(2) check whether the end methods are declared for objects that are no longer referenced. If time permits, the garbage collection process may start to process these existing end methods.

(3) After all termination methods are executed, the garbage collector must start from the root node and detect objects that are no longer referenced (the second scan ). Because the termination method may have revived some objects that are no longer referenced, so that they are referenced again.

(4) The Garbage Collector releases objects that are not referenced in the first and second scans.

(5) to reduce the time needed to release the memory, the garbage collector can selectively insert a step between scanning to some objects with the termination method and executing the termination method: run a small trace from the object that needs to execute the termination method. Objects that are not touchable during the first scan, and are not touchable for small tracking, cannot be revived and immediately released.

(6) If an object with an termination method is no longer referenced and its termination method has been run, the garbage collector must remember it. If this object is revived by the end method of itself or other objects and is not referenced again later, the garbage collector cannot execute the end method of this object again, instead, it should be treated as an object without an ending method.

(7) It is the final method of the object running the garbage collector.

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.