Garbage collector and memory allocation policy

Source: Internet
Author: User

  After more than half a century of development, the current memory dynamic allocation and memory recovery technology is quite mature, but as a program ape still need to understand the GC and memory allocation. When you need to troubleshoot various memory overflows, memory leaks, and when garbage collection becomes a bottleneck for the system to achieve higher concurrency, it is necessary to monitor and regulate the dynamic allocation of memory and the memory recovery technology.

This paper Apple told the collection of garbage in memory and the strategy of memory allocation. In comparison, garbage collection is more difficult. This article will introduce several common garbage collectors and common garbage collection algorithms. Garbage collection algorithm is based on judging whether the object is dead in memory, only to determine that the object has died, in order to take different ways to collect and realize the recovery of memory.

common methods of judging object death
    • Reference counting algorithm (Reference counting): Add a reference counter to the object, and whenever there is a reference to it, the counter value is + 1, and when the reference fails, the counter value is 1; the object with counter 0 at any time is impossible to be used again. The realization is simple, the judgment efficiency is high . It is difficult to solve the problem of circular reference between objects , and there is no counting algorithm in mainstream Java Virtual machine to manage memory.
    • Accessibility Analysis Algorithm (reachability analyses): Through 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 (Reference Chain), When an object is connected to a GC roots without any reference chain, it proves that the object is not referenced. even if the object is unreachable, it is not "immortal ", but temporarily in the "probation" stage, the real declaration of death, or at least experience the re-tagging process: If the object finds no reference chain connected to the GC roots after the accessibility analysis, will be marked for the first time and filtered once, the condition is whether this object is necessary 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". In the Java language, there are four objects that can be used as GC roots: (1) objects referenced in the virtual machine stack, (2) objects referenced by class static properties in the method area, (3) objects referenced by constants in the method area, and (4) objects that are referenced by JNI in the local method stack.

Determines whether an object is alive relative to "reference." References can be divided into strong references (strong Reference), soft references (Soft Reference), weak references (Weak Reference), virtual references (Phantom Reference) four, and their reference strength is reduced in turn.

garbage Collection Algorithm
    • mark-Clear Algorithm (mark-sweep): Divided into "Mark" and "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. Two main deficiencies: One is the efficiency problem , the efficiency of marking and clearing two processes is not high, the second is the space problem , after the mark is cleared, it produces a lot of discontinuous memory fragments.
    • copy Algorithm (Copying): Divide available memory by capacity into two blocks of equal size, use only one piece at a time, and when this piece is used up, copy the surviving object to the other, then clean up the used memory space once. Two blocks of memory can be divided into different proportions according to actual requirements. Commercial virtual machines are now using this collection algorithm to reclaim the new generation . When the object has a higher survival rate, more replication is required and the efficiency becomes lower.
    • mark-and-Organize algorithm (mark-compact): The process of tagging is the same as the "mark-sweep" algorithm, except that all surviving objects are moved to one end in the grooming phase, and then the memory outside the end boundary is cleaned up directly.
    • Generational collection algorithm (generational Collection): According to the different life cycle of the object divides the memory into several blocks, generally divides the Java heap into the Cenozoic and the old age, then uses the most appropriate collection algorithm according to the characteristic of each age.

garbage collector

The garbage collector is a specific implementation of memory reclamation. A hotspot virtual machine that is based on JDK 1.7 Update 14 is included as shown in the collector:

    • Serial collector : A single-threaded collector that uses only one CPU or one thread to complete garbage collection, and when garbage collection, all other worker threads must be paused for direct collection to end. This is a good choice for virtual machines running in client mode .
    • parnew collector : Multi-threaded version of the serial collector. The first Cenozoic collector in a virtual machine running in server mode. With the exception of the serial collector, only the Parnew collector can work with the CMS collector at this time.
    • Parallel Scavenge collector : Also known as the "throughput first" collector, the focus is to achieve a controllable throughput (throughput). throughput = Run user code time/(Run code time + garbage collection time). The Adaptive Tuning Strategy is also an important difference between the parallel scavenge collector and the Parnew collector. GC Adaptive Throttling Policy (GC ergonomics) refers to the virtual machine collects performance monitoring information according to the current system operation, dynamically adjusts the parameter-xx:+useadaptivesizepolicy to provide the most appropriate pause time or maximum throughput.
    • Serial old collector: single-threaded collector, using the "Mark-organize" algorithm. The main meaning is to use the virtual machine in client mode.
    • Parallel Old collector : is an older version of the Parallel scavenge collector, using multi-threaded and "mark-and-organize" algorithms that focus on throughput and CPU resource sensitivity The parallel scavenge plus parallel old collector can be considered as a priority.
    • cms collector (Concurrent Mark Sweep): collector that targets the shortest payback time. The entire process can be divided into initial tags (cms initial mark), concurrent tagging (CMS concurrent mark), re-tagging (CMS remark), Concurrent Purge (CMS concurrent sweep) 4 steps. The longest time in the process is the concurrency tag and the concurrency cleanup process, but they all work with the user thread. Overall, the memory recycling process for the CMS collector is performed concurrently with the user thread. There are 3 obvious drawbacks: (1) The CMS collector is very sensitive to CPU resources, (2) The CMS collector is unable to handle the floating garbage (floating garbage), and (3) there is a lot of space debris at the end of the collection.
    • G1 Collector (Garbage-first): One of the most advanced achievements in today's collector technology development. The Collector collection before G1 is the whole new generation or the old age, G1 is no longer the case. G1 divides the entire heap into separate regions of equal size (region), G1 tracks the value of the garbage accumulation in each area (the amount of space collected and the experience of the time it takes to reclaim), maintains a prioritized list in the background, and each time according to the allowable collection times, Priority is given to recovering the region with the greatest value, thus ensuring that the G1 collector can obtain the highest possible collection efficiency in a limited amount of time. The run is divided into 4 steps: The initial tag (Initial marking), the concurrency tag (Concurrent marking), the final tag (final marking), and the filter collection (Live Data counting and evacuation). Features include parallel and concurrency, generational collection, spatial integration, predictable pauses.

memory allocation and recovery policy
    • objects take precedence over Eden Allocations : In most cases, objects are allocated in the New Generation Eden area. When the Eden area does not have enough space to allocate, the virtual machine will initiate a minor GC.
    • large objects go directly to the old age : Large objects are Java objects that require a lot of contiguous memory space, long strings and arrays are the most typical large objects.
    • long-lived objects will enter the old age : Virtual machines Define an Object age counter for each object. If the object is still alive after Eden was born and after the first minor GC, and can be accommodated by survivor, it will be moved to survivor space, and the object age is set to 1. Objects in the Survivor area each "through" a minor GC, age increased by 1 years, when the age reached a certain level, will be promoted to the old age. The promotion threshold can be set by parameter.
    • Dynamic Object Age : In order to better adapt to different memory conditions, the virtual machine is not always required to be the age of the object must reach Maxtenuringthreshold in order to promote the old age. If the sum of all objects of the same age in the survivor space is greater than half the size of survivor space, objects older than or equal to that age can enter the old age directly.
    • Space Allocation guarantee : Before the minor GC occurs, the virtual opportunity checks whether the largest available contiguous space in the old age is greater than the total space of all new generation objects to ensure that the minor GC is secure. If not, check to see if the Handlepromotionfailure setting value allows the warranty to fail. If allowed, will continue to check whether the largest available continuous space in the old age is greater than the average size of the previous promotion to the old age object, if greater than, will try to do a minor GC, but there is a certain risk; if less than, or handlepromotionfailure setting does not allow adventure, Then one full GC is performed instead.

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.