Java Virtual machine

Source: Internet
Author: User

Java garbage Collection (GC) mechanism

tags (space delimited): Java Virtual machine

What is garbage collection?
The objects we need to recycle are those that can't be used in any way anymore.

How do I determine that an object is no longer referenced?
1. Reference counting Method:
Add a reference counter to the object, each time a place references the object once, the counter value is + 1, and when the reference is invalidated-1. When the count value equals 0, the object is no longer referenced
The algorithm is simple and efficient, and it is a good algorithm in most cases. But its big drawback is that it can't solve the problem of mutual reference between objects.

/*** 虚拟机参数:-verbose:gc*/public class ReferenceCountingGC{    private Object instance = null;    private static final int _1MB = 1024 * 1024;        /** 这个成员属性唯一的作用就是占用一点内存 **/    private byte[] bigSize = new byte[2 * _1MB];        public static void main(String[] args)    {        ReferenceCountingGC objectA = new ReferenceCountingGC();        ReferenceCountingGC objectB = new ReferenceCountingGC();        objectA.instance = objectB;        objectB.instance = objectA;        objectA = null;        objectB = null;                System.gc();    }}

Operation Result:

[GC 4417K->288K(61440K), 0.0013498 secs][Full GC 288K->194K(61440K), 0.0094790 secs]

From the running results, two objects refer to each other, but the virtual machine still reclaims two objects, which means that the virtual machine does not use the reference method to determine whether an object is alive or not.

2. Accessibility algorithm Analysis
The basic idea of this algorithm is to use a series of objects called "GC Roots" as a starting point, search down from these starting points, the path of the search is called the reference chain, when an object to the GC Roots no reference chain, it proves that this object is not available.
The objects in Java that can be used as GC roots include:
Objects referenced in the virtual machine stack
Objects referenced by static properties in the method area
Objects referenced by constants in the method area
Objects referenced by JNI (native method) in the local method stack

Reference Status:
Strong references, soft references, weak references, virtual references
Prior to JDK1.2, Java's definition of references was that there were only two states of an object (both referenced and not referenced); but now we do it when the memory space is sufficient, the object is preserved, and if the memory space is still very tense after garbage collection, you can discard these objects---JDK1.2 the extension of the reference concept.

Garbage collection algorithm
1. Mark-Sweep algorithm
2. Copy algorithm
3. Marking-Sorting algorithm
4, generational collection algorithm (that is, the above three kinds of algorithms to integrate, under the appropriate conditions to use the corresponding algorithm)

Garbage collector
The garbage collector is a concrete embodiment of the above theory. The hotspot virtual machine we use contains a garbage collector such as:

There are 7 collectors, divided into two, above the new generation collector, the following is the old age collector. Wired between the two can be used in conjunction with

Concurrency and parallelism
Parallel (parallel): Multiple garbage collector threads work in parallel, but the user is still in a wait state
Concurrency (Concurrent): The user executes concurrently with the garbage collector thread, the user program continues to run, and the garbage collector program runs on the other CPU.

Serial (serial GC) collector
The serial collector is a new generation collector, single threaded execution, using a copy algorithm. It only uses one CPU to collect the garbage, and the other worker threads must wait and know that it is collected to complete.
Parnew (parallel GC) collector
The Parnew Collector is a multithreaded version of the serial collector, using a copy algorithm. In addition to using multiple threads for garbage collection, the rest behaves like the serial collector.

Java Virtual machine

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.