Java Memory management-JVM garbage collection

Source: Internet
Author: User
I. Overview

Java is much more convenient than recovering memory from C and C + +, because the JVM automatically allocates memory and reclaims memory for us.

In previous JVM storage management, we introduced several areas of JVM memory management where program counters and virtual machine stacks were thread-private and were out of the thread, so it was not considered garbage collection, because the thread ended its memory space as freed.

The Java heap and the method area are different, and the Java heap and the method area hold the object's instance information and other information about the object, which is the main place of garbage collection.

two. Java Heap garbage collection

There are two main considerations for garbage collection: one is efficiency and one is space debris.

Garbage collection In the Java heap can be divided into two regions, one is the Cenozoic, the other is the old age. The Cenozoic is divided into a larger Eden space and two smaller survivor spaces. Because the new generation and older generations of the object group is not the same, in order to achieve a balance between efficiency and space debris problems, the new generation and the old generation of garbage collection algorithm used is not the same.

Cenozoic-Replication algorithm

It is known from the name that the new generation mainly stores the newer objects, and the objects that are still alive after many times will be sent to the old Central District. Therefore, the new generation of garbage collection is more frequent, so in order to solve the efficiency problem, the new generation uses the replication algorithm. The replication algorithm can divide memory into two blocks of equal size, using one chunk at a time, and when this piece is exhausted, copies the surviving objects to another area of memory. This piece of memory that has been used at this time can be cleaned up once, so there is no need to worry about memory fragmentation. Of course, one drawback of this algorithm is that memory usage is low, and only half (half of each time is allocated).

And IBM's research shows that the new generation of object 98% is "shining in the dead", so do not need to divide according to 1:1, it will be divided into a larger Eden space and two small survivor space.

So why would there be two survivor, the copy algorithm is not only need a piece of Eden and a piece of survivor is enough?

In fact, this is mainly to solve the problem of fragmentation. Assuming there is only one survivor zone, when the Eden is full, the GC is carried out, and the surviving objects are assigned to the Survivor area, emptying the Eden area. When the GC is complete again, the surviving object continues to be placed in the Survivor area, which is not very good, no memory fragmentation Ah! But don't forget that the first time the object to the survivor area is likely to be inactivated at the time of the second GC, clearing out the Survivor object will not produce memory fragmentation?

So the Java heap uses two survivor zones, one from Survivro and one Tosurvivor, the first time Eden is full, the copy algorithm places the surviving objects in the from Survivor area, emptying Eden. The second time, when Eden is full, place the surviving objects from the Eden and from Survivor into the to Survivor area, emptying the Eden and from Survivor, and then swapping the from Survivor and to Survivor roles in an important step! This solves the problem of memory fragmentation.

Old age-marker/collation algorithm

The first thing to understand is that the old age is the object that will survive longer, so if the old age also uses the copy algorithm, then the cost of copying objects is relatively large, because the old age of the object will basically survive.

The tagging/grooming algorithm is well understood, primarily the "tagging", "grooming" two steps, first the object to be recycled is tagged, then the surviving object moves toward one end, then the memory outside the boundary, then the GC completes.

three. Method Area garbage Collection

In some parts of the interpretation, the method area will also be called "permanent generation", unlike the Java heap, where the information is stored in the class and some constant information, so the allocated memory in this area is generally more difficult to be recycled, so there is a "permanent generation" name.

Although garbage collection is less efficient in the method area, the allocated memory is not really never recycled, and its main recycling is two parts: obsolete constants and useless classes. Recycling of obsolete constants is similar to the collection of class instances in the Java heap, which can be recycled when a constant in a constant pool is not referenced. For example, there is a string constant "abc" in a constant pool, and when there is no string object with the value "ABC", the Next garbage collection "ABC" constant is likely to be recycled.

In the case of garbage collection, the first thing to judge is what kind of class is "useless":

    • All instances of the class have been reclaimed, that is, there are no instances of the class in the Java heap.
    • The ClassLoader of the loaded class has been recycled.
    • The corresponding Java.lang.Class object of this class is not referenced anywhere and cannot be used to access the class's methods at any place.

Virtual machines may be able to heap the "useless classes" that meet these three conditions for recycling, which is only possible, not necessarily.

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.