Deep understanding of jvm-garbage collector and memory allocation policy

Source: Internet
Author: User

In the previous article, the Java Memory Runtime area is described, where the program counter, virtual machine stack, local method stack 3 zones are born out of the thread, and stack frames in the stack are methodically executed into the stack as the method enters and exits. The amount of memory allocated in each stack frame is basically known as the class structure, so the allocation and recycling of memory in these areas is deterministic. The memory is reclaimed when the method is accepted. Java heap and good method area is not the same, multiple implementations of an interface need memory may not be the same, multiple branches of a method may require different memory, we can only know when the program is running to create which objects need to be created. This part of the memory allocation and recycling are dynamic, the garbage collector is the focus here.

Determine if the object is alive:

One: Reference counter method

To add a reference counter to the object, whenever there is a place to add a reference, the reference fails to subtract one, the counter is 0 will not be able to use in the second. But there is a problem, the circulation problem cannot be solved.

Two: Accessibility analysis algorithm

The basic idea of this algorithm is to search through a series of objects that become "GC Roots" as starting points, search for the path that has been taken as a reference chain, and the title object is not available when the GC Roots of an object does not have any chain necklace.

In Java, the objects that can be used as gcroots include:

    • Objects referenced in the virtual machine stack
    • Variables referenced by static properties in the method area
    • Objects referenced by constants in the method area
    • The object referenced by JNI (generally referred to as the native method) in the local method stack

Reference

Regardless of which method to judge, eventually there is a reference, before JDK1.2, defined if the reference type of data stored in a value represents a different piece of memory of the starting address, the memory represents a reference. After JDK1.2, the references are expanded to include strong references, soft references, weak references, and virtual references.

    • Strongly quoted, ubiquitous, similar to object obj = new Object (), which does not reclaim the referenced object if it exists
    • Soft references, which describe some useful but not necessary objects. Before the system is going to have a memory overflow exception, these objects will be listed in the Reclaim range for two recoveries, and a memory overflow exception will be thrown if the collection does not have enough memory.
    • A weak reference is also an object that describes useful but not necessary. But it's a little bit more than a soft reference. Objects that are referenced by a weak reference can only survive until the next garbage collection, and these objects will be reclaimed, regardless of whether the memory is sufficient.
    • Virtual references, whether an object has a virtual reference, does not have an impression of its time-to-live. You cannot get an object based on a virtual reference, and the purpose of the virtual reference is notified by a system before the object is recycled.

Even after the accessibility analysis algorithm is not able to reach the object, it is not dead, when they temporarily out of a reprieve, is really an object of death, at least two times to go through the marking process: if the object after the accessibility analysis did not find the GC roots linked chain of reference, then it will be marked the first time, and filtering in turn, the criteria for filtering is that there is no need to execute the Finalize () method, both of which are considered unnecessary when the object does not overwrite the Finalize method or the Finalize method has been called by the virtual machine.

If it is determined that the Finalize method is necessary, then this object will be placed in a queue called F-queue, and at a later time the virtual machine automatically establishes a low-priority finalizer thread to execute. The execution here only guarantees that the virtual machine goes to the trigger, and does not guarantee that it will wait for the end of execution, because if the Finalize method of an object is slow or a dead loop occurs, the F-queue queue other objects are permanently waiting. The Finalize () method is the last chance that an object escapes death. Later, the object in F-queue is marked with a second small size, if the object is to successfully resolve itself in the Finalize () method-just re-associate with how an object on the reference chain. If this is assigned to a variable, it will be removed from the collection that is about to be reclaimed at the second recycle, and if it does not escape, it will basically be recycled. Note here that it is not recommended to manually revive the object in the Finalize method.

Recycling Method Area

Many people think that the method area (or the permanent generation of the Hotspot virtual machine) is not garbage collected. Java Virtual Machine specification also said that the virtual machine can be required in the method area does not implement garbage collection, and in the method area of garbage collection is relatively low cost, in the heap, especially in the new generation, the general application of garbage collection can be recycled%70-%95 space.

The permanent garbage collection is divided into two parts, solid and useless classes. Discard constant means that if a string "a" has entered a constant pool, but the current system does not have any string object pointing to it, there is no other reference to the literal, if this is garbage collection, then the string will be cleaned out of the constant pool, the field symbol reference is similar.

To determine whether a class is useless, you need to meet 3 conditions:

    1. All instances of the class have been reclaimed, that is, no instances of the class exist in the Java heap.
    2. The ClassLoader that loaded the class have been recycled
    3. The Java.lang.Class object of the modified class is not referenced anywhere. The method of the class cannot be found anywhere by reflection.

This is also just "yes", and whether the class is recycled, the hotspot virtual machine provides-XNOCLASSGC parameter control. You can also use-verbose:class and-xx:+traceclasscoading (product version of virtual machines),-xx:+traceclassunloading (Fastdebug virtual machines) to view class loading and uninstallation.

Junk phone algorithm

Tag-purge algorithm (mark-sweep)

The underlying collection algorithm is the "tag-purge" (mark-sweep) algorithm. Like its name, the algorithm is divided into "mark" and "clear" two stages: first mark out all the objects that need to be recycled, after the completion of the tag to collect all the tagged objects, the reason is that it is the most basic collection algorithm, because the subsequent collection algorithms are based on this idea and to improve their shortcomings. There are two main deficiencies: one is efficiency, marking and clearing two processes is not efficient: the other is the space problem, marking and Cheongju in addition to generating a large number of discontinuous memory fragments, too much space debris may cause later in the run process need to allocate a large object, Unable to find enough contiguous memory to trigger another garbage collection action ahead of time

Copy

To solve the problem, a collection algorithm called "Replication" (copying〉), which divides the available memory by capacity into two blocks of equal size, uses only one chunk at a time, and when this piece of memory is used up, copies the surviving objects to the other. The used memory space is then cleaned up once, so that the entire half of the memory is reclaimed each time. Memory allocation also do not consider the complexity of memory fragmentation, as long as the mobile heap top pointers, in order to allocate memory, the implementation of simple, high efficiency, but the cost of this algorithm is to reduce the memory of the original half, it is a little too much

Tag Grooming

According to the characteristics of the older generation, it was suggested that the mark-and-Finish (mark-compact) algorithm, the marking process is still the same as "mark-clear", but the next step is to let all the surviving objects moving to one end. Then clean out the unexpected memory on the side.

The algorithm of generational phone

The current commercial virtual machine garbage collection algorithm uses the Generational collection algorithm, according to the object's survival period divides the memory into several blocks, generally divides the Java heap into the Cenozoic, the old generation. So according to the characteristics of each era of different algorithms, the new generation, each time there are a large number of objects to die, only a small number of survival, then the use of replication algorithm. Older generations have a high survival rate, and no additional motion checks guarantee it, using a "mark-sweep" or "mark-sweep" algorithm for recycling.

Deep understanding of jvm-garbage collector and memory allocation policy

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.