Garbage collection in Java

Source: Internet
Author: User

The main two steps for garbage collection are:

    1. The judgment of the garbage object
    2. Recycling of garbage objects
Methods of judging garbage objects
    1. Reference counting algorithm: Add a reference counter to the object, and whenever there is a place to reference it, the counter value is incremented by 1, and when the reference fails, the counter value is reduced by 1, and any object with a counter 0 at any time is a garbage object.
    2. Accessibility analysis algorithm: Through a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, when an object to the GC Roots no path connected ( in the case of graph theory, is from the GC Roots to this object unreachable ), Then this object is a garbage object.

So, what objects can be thought of as "GC Roots" objects, as follows

    • Class-Objects loaded by the system ClassLoader that cannot be recycled, and can hold other objects in a static field.
    • Thread-Alive Threads
    • The local variable or parameter of the Stack local -Java method
    • Local variable or parameter of the JNI local -Jni method
    • JNI Global-Global JNI reference
    • Monitor used -monitoring objects for synchronization
    • Held by JVM -objects reserved for the special purpose of the JVM by the GC, but this is actually related to the implementation of the JVM. Some of the types that may be known are the system ClassLoader, the important exception classes that some JVMs know, some pre-allocated objects for handling exceptions, and some custom class loaders. However, the JVM does not provide additional information for these objects, so it is only left to the analyst to determine what belongs to the "JVM hold".
Garbage Object Recycling Method

Let's take a look at three algorithms for garbage collection.

Replication Algorithms

Divide available memory by capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is exhausted, copy the surviving object to the other piece, and then clean up the used memory space once.

"Mark-Clear" (mark-sweep) algorithm

First, all objects that need to be recycled are marked, and all tagged objects are collected uniformly after the mark is completed;

It has two deficiencies: one is the efficiency problem, the labeling and elimination of two processes is not efficient, and the other is a space problem, the mark after the removal of a large number of discontinuous memory fragmentation;

Tagging-sorting algorithms

The tagging process is still the same as the tag-purge algorithm, but the next step is not to clean up the recyclable objects directly, but rather to have all the surviving objects move toward one end, and then directly clean out the memory outside the end boundary.

So, how are garbage objects recycled? The answer is generational recycling.

Generational recycling Strategy

The life cycle of different objects is not the same. Therefore, different life cycle objects can be collected in different ways to improve the efficiency of recycling. Generational garbage collection adopts the idea of divide and conquer, divides the generations, puts the objects of different life cycles on different generations, and uses the garbage collection method which is most suitable for it in different generations.

In general, the Java heap is divided into the new generation and the old age, so you can use the most appropriate collection algorithm according to the characteristics of each age.

In the Cenozoic, there are a large number of objects died, only a small number of survival, the general choice of replication algorithm, only a small number of surviving objects can be copied cost to complete the collection. The Cenozoic is divided into three districts. An Eden area, two survivor districts (rescue space, in general). Most objects are generated in the Eden area, and when the Eden Zone is full, the surviving objects are copied to the Survivor area (one of two), and the memory of the newly created object is allocated from Eden,minor collection, which is the process of Eden and the survivor The live object in space copy to the idle Survivor space, the object in the Cenozoic experienced a certain number of times (can be configured through the parameters) minor collection, will be moved to the old age, known as tenuring.

In the old age, because of the high survival rate of objects, there is no need to clean up or tidy up a lot, so use a "tag-clean" or "mark-sweep" algorithm to recycle.

GC is divided into two types, scavenge GC and full GC;

    • Scavenge GC: GC for Eden Zone, clears non-surviving objects, and moves surviving objects to the survivor area.
    • Full GC: GC for the entire heap, including young, tenured, and perm. In the process of tuning the JVM, a large part of the work is to adjust the FULLGC. Minimize the number of full GC

The full GC may be caused by the following reasons:

    • The old generation (tenured) was written full
    • Persistent generation (Perm) is full
    • System.GC () is displayed call
    • Dynamic changes in the domain allocation policy of the heap after the last GC

Reference:

"In-depth understanding of Java virtual machines"

http://blog.csdn.net/fenglibing/article/details/8928927

Garbage collection in Java

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.