Garbage collector and memory allocation policy

Source: Internet
Author: User

Read the "Deep Understanding Java Virtual Machine" Art Chapter III, "Garbage collector and memory allocation strategy", to copy the important concepts in this chapter for subsequent memory and understanding.

In the heap, where almost all of the object instances in the Java world are stored, the first thing the garbage collector can do before reclaiming the heap is to determine which of these objects is "alive" and which ones are "dead" (objects that can no longer be used by any means).

Is the object dead?Reference counting algorithm

Concept: Add a reference counter to an object, the counter value is incremented by 1 whenever there is a reference to it, and when the reference fails, the counter is reduced by 1, and any object with a counter 0 at any time is impossible to use again. However, there is no reference counter algorithm in the mainstream Java virtual machine to manage memory, the main reason is that it is difficult to solve the problem of mutual bad reference between objects.

Accessibility analysis algorithm

The basic idea is to use a series of objects called "GC Roots" as the starting point, starting from these nodes to search down, the path of the search is called the reference chain, when an object to the GC Roots no reference chain connected, it proves that this object is not available. In the mainstream implementation of the mainstream Business programming language (java,c#), the accessibility analysis is used to determine whether an object survives.

In the Java language, objects that are available as GC roots include the following

    • The object referenced in the virtual machine stack (the local variable table in the stack frame).

    • Object referenced by class static property in method area

    • The object referenced by the constant in the method area.

    • The object referenced by JNI (that is, generally speaking, the native method) in the local method stack.

Talk about references again

After jdk1.2, Java extends the concept of references into strong references (strong Reference), soft references (Soft Reference), weak references (Weak Reference), virtual references (Phantom Reference ) of 4, these 4 reference intensities gradually weaken in turn.

    • A strong reference is a reference that is common in program code, such as "Object obj = new Object ()", as long as a strong reference exists, and the garbage collector never reclaims the referenced object.

    • Soft references are used to describe objects that are useful but not necessary. For objects associated with soft references, these objects will be listed in the Reclaim scope for two recoveries before the system will have a memory overflow exception, and a memory overflow exception will be thrown if the collection does not have enough memory. After JDK1.2, the class SoftReference class is provided to implement the soft reference.

    • A weak reference is also used to describe a non-required object, but its strength is weaker than a soft reference, and the object associated with the weak reference only survives until the next garbage collector. When the garbage collector is working, the objects associated with a weak reference are reclaimed regardless of whether the current memory is sufficient. After JDK1.2, a weakreference is provided to implement a weak reference.

    • A virtual reference is the weakest reference relationship. Whether an object has a virtual reference exists, does not affect its lifetime at all, and cannot obtain an object instance through a virtual reference. The only purpose of setting a virtual reference association for an object is to be able to receive a system notification when the object is reclaimed by the collector. A phantomreference is provided to implement a virtual reference.

      To survive or to die?

Even in the accessibility analysis algorithm unreachable objects, is not "dead", at this time they are temporarily in the "probation" stage, to truly declare the death of an object, at least two times the marking process: if the object after the accessibility analysis found that there is no reference chain connected to the GC roots, Then it will be marked for the first time and filtered to see if it is necessary for this object to execute the Finalize () method. When the object does not overwrite the Finalize () method, or the Finalize () method has been called by the virtual machine, the virtual machine treats both cases as "no need to execute". But it does not encourage you to use the Finalize () method to save the object, which is expensive, uncertain, and does not guarantee the order in which the individual objects are called. Finalize () can do all the work, use try-finally or other ways to do better, more immediately.

Recycling Method Area

The method area (or permanent generation in a hotspot virtual machine) is also garbage collected, and garbage collection mainly recycles two parts: obsolete constants and useless classes. Reclaiming obsolete constants is very similar to recovering objects in the Java heap, and it is relatively harsh to judge whether a class is a "useless class". Class needs to meet the following 3 criteria to be considered a "useless" class:

    • All instances of the class have been reclaimed, that is, no instances of the class exist in the Java heap.

    • The ClassLoader that loaded the class have 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.

      Garbage collection algorithm
Tag-Purge algorithm

The algorithm is divided into "tags", "clear" two stages: first mark out all the objects that need to be recycled, after the mark is complete, all the tagged objects are collected uniformly. Its main deficiencies are two: one is the efficiency problem, the labeling and elimination of two process efficiency is not high, the other is a space problem, the mark after the purge will produce a large number of discontinuous fragments, too much space debris may cause later in the program to run the process of allocating large objects, Unable to find enough contiguous memory to trigger a garbage collection action in advance.

Replication Algorithms

It divides the available memory capacity into two blocks of equal size, using only one piece at a time. When this piece of memory is used up, it assigns the surviving object to the other piece, and then cleans up the used memory space once. This makes every time the entire half of the memory collection, memory allocation, memory fragmentation and other complex situations, as long as the mobile heap top pointers, in order to achieve simple, efficient operation. But the cost of this algorithm is to reduce the memory for the original general, it is a little too high.

Tagging-sorting algorithms

The replication collection algorithm has more replication operation and lower efficiency when the object survival rate is high. What's more, if you don't want to waste 50% of your space, you need extra space to vouch for the extreme situation where all the objects in memory are 100% alive, so you can't use this algorithm directly in the old age. The mark-and-organize algorithm is still the same as "mark-clear", but instead of cleaning up the recyclable object directly, it allows all surviving objects to move at one end and then directly cleans up memory outside the end boundary.

Generational collection Algorithms

The current garbage collector for commercial virtual machines uses a "generational collection" algorithm that divides memory into chunks based on the different lifetime of the object. The Java heap is generally divided into the new generation, the old age. In the Cenozoic, every garbage collection is found to have a large number of objects died, only a small number of survival, then choose the replication algorithm, only need to pay a small number of surviving objects of the replication cost can be completed collection. In the old age, because the object has a high survival rate and no additional space to guarantee it, it must be recycled using the "mark-clean" or "mark-sweep" algorithm.

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.