JVM garbage collection mechanism (1)

Source: Internet
Author: User

Currently Java's JDK default virtual machine is hotspot, so this article involves virtual machine related content refers to the hotspot virtual machine

This article focuses on GC recycling: Determining which objects are recyclable, how they are recycled, and how they are recycled

Determine which objects can be recycled

GC is based on whether the object survives to determine whether or not to recycle, there are two main algorithms to determine whether the object is alive: Reference counting algorithm, accessibility analysis algorithm

    • Reference counting algorithm
      The principle of reference counting is to add a reference counter to the object, each quoted counter plus 1, the reference fails minus 1, when the counter 0 indicates that the object is not referenced, can be recycled, the reference counting method is simple and efficient, but there is a circular reference problem between objects, may cause the GC can not be recycled, It takes a lot of effort to solve the circular reference problem.
    • Accessibility analysis Algorithm -----> Graph Traversal
      The algorithm principle of the accessibility analysis is to start the traversal search all reachable objects from the object root reference (stack, static reference of method table and constant reference area, local method stack), to form a chain of references, to tag both reachable objects and unreachable objects, non-reachable object means no references exist, can be recycled by GC
How to Recycle

How do I recycle after I find a recyclable object?

The memory recovery algorithm mainly has tag-clear, stop-copy, Mark-Tidy, different algorithms use different scenarios, overall stop-copy algorithm suitable for the new generation of short survival time, low survival rate, labeling-clearing and labeling-sorting algorithm suitable for long survival time, high survival age

    • Mark-Clear (Mark-sweep)
      Mark all unreachable objects with the accessibility analysis algorithm, and then clean up the unreachable objects. This algorithm creates a lot of memory fragmentation
    • Stop-Copy (stop-copy)
      The Cenozoic memory is divided into an Eden area and two Survivor (Survivor0,survivor1) regions in accordance with 8:1:1, and the Eden Zone survivor is first copied to a Survivor0 area and then emptied to the Eden area. When the Survivor0 area is fully stocked, the Eden and Survivor0 live objects are copied to another Survivor1 area, then empty the Eden and the Survivor0 area, where the Survivor0 area is empty. The Survivor0 area and the Survivor1 area are then exchanged, that is, to keep the Survivor1 area empty, so that, when the Survivor1 area is not sufficient to store the surviving objects of Eden and Survivor0, the surviving objects are stored directly in the old generation (we may recall, If the old age is also full of what to do, if the old age is full, it will trigger an all-time GC, that is, the new generation, the old generation are recycled, if the memory is not enough ... , not enough that no nonsense, OutOfMemory, not unfamiliar bar haha). From the principle of the stop-copy algorithm, we can see that this algorithm is very efficient for low-survival object recovery, and does not form memory fragmentation, but will waste a certain amount of memory space, suitable for the new generation of low object survival rate, if this algorithm is used in the old age with high object survival rate, It's going to be a disaster.
    • Labeling-Finishing (mark-compact)
      The accessibility analysis algorithm marks all unreachable objects, then moves the surviving objects in one direction and clears out the memory outside the boundary. This algorithm gathers the surviving objects in one Direction and empties the remaining areas, which is suitable for older generations with higher object survival

GC Recovery mechanism: Generational collection algorithm

The JVM memory collection algorithm basically uses the Generational collection algorithm, will divide the memory into the new generation, the old age, also some people put the method area to do the permanent generation

    • Cenozoic
      When objects are created, memory allocations occur in the new generation (large objects are directly allocated in the old age), and most objects are born out of the way, and will soon be unused and become unreachable objects that are reclaimed by GC. The survival rate of the new generation of objects is very low (how low?) Studies have shown that up to 98% of objects are created soon after the extinction, imagine the usual programming, in addition to global variables, local variables in the exit call method after a few can survive, survival time is very short, the new generation of GC is also called minor gc,minorgc frequency ratio is higher ( Not necessarily waiting for the Eden area to be full before triggering)
    • Old age
      When the object in the Cenozoic after the occurrence of multiple minor GC still survive after the object is to enter the old age, the old age more than the number of new objects, of course, the memory is much larger than the Cenozoic (about 1:2, that is, the new generation occupies 1/3 of the total heap of memory), when the old memory full when the major GC Gc,full GC frequency is relatively low, the old age object survival time is longer, survival marker is high

Generally speaking, the GC is in the new generation and the old age, the new generation of objects survival time is short, low survival rate is generally used to stop-copy algorithm, the old age object survival time is long, high survival rate, the general use of marker-collation, marker-clear algorithm, the specific use of the algorithm and the specific use of the garbage collector about

There are some similar minors and adults in the GC, newly created objects for the new generation, the Cenozoic want to become the old age need to undergo a certain growth (a little bit grow up is it), the newly created object age is 1, each occurrence of minor GC, the age of the surviving objects increased by 1, after 15 times minor GC, The surviving target is 15 years old, reaching the age of majority of 15 years (the default is 15), officially become an adult (the old age), the object of adult life will not have the concept of age, until the object of death, will stay in the old age, of course, there are some old immortal (static variables, constants, etc.), and the world will (GC crashes)

GC Collector

GC After the use of generational recovery, plays an important role is the GC collector, GC Collector is divided into the new generation collector and the old age collector, different collectors use different collection algorithms, have different characteristics, because the current collector in memory recovery can not eliminate (stop-the-world), That is, in the recovery of memory inevitably stop the user thread, the current collector can only make the pause time is shorter, but can not be completely eliminated, the main collection of which parallel scavenge and parallel old is the pursuit of throughput as the goal, other collectors are the pursuit of high response, low pause,

  Cenozoic Collectors : Serial, pranew, Parallel scavenge

  old age collector : Serial, Parallel, CMS

    • Serial collector (copy algorithm)
      The new generation of single-threaded collectors, marking and scavenging are single-threaded, with the advantage of being simple and efficient.
    • Serial Old collector (Marker-Collation algorithm)
      The old age single-threaded collector, the old age version of the serial collector.
    • parnew Collector (Stop-copy algorithm)
      The new generation collector, which can be considered a multithreaded version of the serial collector, has a better performance than serial in multi-core CPU environments.
    • Parallel Scavenge collector (Stop-copy algorithm)
      A parallel collector that pursues high throughput and uses CPUs efficiently. Throughput is typically 99%, throughput = user thread time/(user thread time +GC thread time). Suitable for background applications, such as the corresponding requirements for the interaction of the scene is not high.
    • Parallel Old collector (Stop-copy algorithm)
      Parallel scavenge collector's old version, parallel collector, throughput priority
    • CMS (Concurrent Mark Sweep) collector (tag-cleanup algorithm)
      High concurrency, low pause, the pursuit of the shortest GC recovery pause time, high CPU consumption, fast response time, short pause time, multi-core CPU to pursue high response time selection
    • G1 (Garbage-first) collector (marker-collation algorithm, stop-copy algorithm)
      GC Latest model, Gaofu, high concurrency, predictable pauses, generational collection, no unexpected future mainstream will gradually replace the concept of cms,g1, although still divided into the new generation and the old age, but the new generation and the old age is no longer physical isolation, but the memory is divided into N region, With region as the clean-up unit, the overall use of the labeling-sorting algorithm, region internal using stop-copy algorithm.

JVM garbage collection mechanism (1)

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.