Garbage collector in Java

Source: Internet
Author: User

Any language that creates objects in the process of running it means that they need to allocate space in memory for these objects, and when those objects lose their meaning, they need to be freed up to make sure that the memory is available to the new object. The release of object memory is a garbage collection mechanism, also called a GC, for Java developers, GC is a double-edged sword. We've got two funny pictures here to show the C-language garbage collection and Java garbage collection. Note: Not to say who is good who is bad, just a mocking figure.

C Language:

Java language:

C Garbage Collection is artificial, the workload is large, but the controllability of high. Java is automated, but the controllability is poor, and sometimes there is a memory overflow, memory overflow that is, the JVM allocates too many objects in memory, exceeding the maximum allocated memory size.

There is a way to mention the Java garbage collection mechanism:

System.GC () is used to invoke the garbage collector, and when called, the garbage collector runs to reclaim unused memory space.  It attempts to free memory that is consumed by discarded objects.  However, the System.GC () call comes with a disclaimer that does not guarantee a call to the garbage collector. So System.GC () does not say that it is a perfect initiative for garbage collection. As a Java programmer it is necessary to understand the GC, which is often a topic in the interview process.

We understand the GC from three angles:

1JVM how to determine which objects should be recycled

When the 2JVM will take the garbage collection action

3JVM exactly how to know the garbage object

How the JVM determines which objects should be recycled

The two classic algorithms that an object will be recycled: reference counting , and accessibility analysis algorithms .

Reference counting method

In simple terms, the number of references to the object is judged. Implementation method: To add a reference counter to the object, whenever there is a reference to him, the value of the counter is added 1, when the reference fails, that is, the object is not executed, the value of his counter is reduced by 1, if the value of the counter of an object is 0, then it means that the object has no one to refer to him,  This means that a failed garbage object will be recycled by the GC.  But this simple algorithm is not used in the current JVM because he does not solve the problem of circular references between objects. Suppose there is a and B two objects referencing each other, that is, a property in a object is a property in B,b when a, in this case because of their mutual reference, which is not recognized by the garbage collection mechanism.

Because of the disadvantage of the reference counting method, the Accessibility analysis algorithm is introduced to determine whether the object can be recycled by judging whether the object's reference chain can be reached. The Accessibility analysis algorithm is introduced from the graph theory of discrete mathematics, and the program regards all referential relationships as a graph, starting with a series of objects named GC roots, from which the search path is called the reference chain. When an object is not connected to the GC Roots (that is, from the GC Roots to the object unreachable), it proves that the object is not available.

Second, after determining which objects can be recycled

When will the JVM be recycled?

1 will be automatically reclaimed when the CPU is idle 2 after the heap memory storage is full 3 actively call System.GC () after attempting to recycle

Three How to recycle

How to Recycle said garbage collection algorithm.

There are four algorithms: tag-purge algorithm , copy algorithm , marker-collation algorithm , and generational collection algorithm .

1 mark-Clear algorithm. This is the most basic of an algorithm, divided into two steps, the first step is the tag, that is, all the objects that need to be collected at the mark, after the completion of the token, the unified collection of which tagged objects. The advantage of this algorithm is simple, the disadvantage is the efficiency problem, there is a big drawback is the space problem, the mark after the purge will produce a large number of discontinuous memory fragmentation, when the program in the future to allocate large objects in the process of allocating a larger object can not find enough contiguous memory, resulting in memory space wasted. Perform:

2 copy algorithm. Replication divides the 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. This makes it possible to make a memory collection of one piece at a time, and memory allocations do not take into account complex situations such as memory fragmentation. Only the cost of this algorithm is to reduce the memory to half the original. The execution process of the replication algorithm:

The replication collection algorithm performs more replication operations when the object has a higher survival rate and becomes less efficient. More crucially, half of the space is wasted. Tag-Finishing algorithm: The tagging algorithm is similar to the mark-clearing algorithm, but the most notable difference is that the tag-clearing algorithm only deals with objects that are not surviving, and the remaining surviving objects do not do any processing, which causes memory fragmentation, while the tag grooming algorithm not only processes the non-surviving objects, but also organizes the remaining surviving objects. Re-organized so that it does not produce memory fragmentation. The function of the marker grooming algorithm is as follows:

Generational collection algorithm: Generational collection algorithm is a more intelligent algorithm, is now the most used by the JVM algorithm, he is actually not a new algorithm, but he will be in the specific scene automatically select the above three algorithms for garbage object recycling. So now the focus is the generation of the collection algorithm said automatically according to specific scenarios to choose. What exactly is the scene of this particular scenario. The scenario actually refers to which area of the JVM the JVM divides the memory into three areas before 1.7: The new generation, the old age, and the permanent generation.

After understanding the scene and then combined with generational collection algorithm to draw the conclusion: 1, in the Cenozoic, each garbage collection found a large number of objects died, only a small number of survival, then choose the replication algorithm. The collection can be done with only a small amount of replication costs for the surviving objects. 2, the old age because the object survival rate is high, there is no extra space for him to be assigned to guarantee, it must be marked-clear or marked-collation. Summarize:

Note: Java discards the permanent generation at JDK8, but it does not mean that our conclusions fail because Java provides a technique called "meta-space" similar to a permanent generation. Abandoned permanent generation Reason: because the permanent generation of memory is often not enough or a memory leak, the exception Java.lang.OutOfMemoryErroy. The nature of meta-space is similar to the permanent generation. However, the biggest difference between meta-space and permanent generation is that the meta-space is not in the virtual machine, but rather uses local memory. That is, the memory of the system can be used without limitation with the JVM. It is theoretically dependent on the virtual memory size of the 32-bit/64-bit system.

Garbage collector in Java

Related Article

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.