In-depth understanding of Java Virtual Machine garbage Collection II

Source: Internet
Author: User

garbage collector

shown as all garbage collectors used by the hotspot virtual machine after JDK1.7 Update14

If the two collectors are connected, the two collectors can be used with each other. The area where the collector is located indicates whether it is a new generation collector or an old collector.

Serial collector

Main See figure ↑ demo serial/serial old collector run. The serial collector is a single-threaded collector that must be paused while the collector is garbage collected (Stop the world!) Other worker threads (this is like when your mother is cleaning the room, she will certainly let you stay on the side, if she cleans, you throw confetti, this room how to clean it?) )。 Serial is simpler and more efficient than other new generation collectors (single-threaded and multiline threads are less expensive to thread-interactive), so now it's still the default Cenozoic collector in Client mode.

parnew Collector

Demonstrates the Parnew collector run. The Parnew collector is actually a multithreaded version of the serial collector, and the other and serial collectors have no essential gaps, and if you want to find a different point, emmm, it is the preferred Cenozoic collector in a virtual machine running in server mode. The Parnew collector does not necessarily perform better than serial in a single CPU environment, but as the CPU cores increase, its advantages are revealed.

Parallel Scavenge collector

Before introducing the parallel scavenge collector, I first defined a basic concept for the reader: throughput = time to run user code/(time to run user code + garbage collection Time), for example, virtual machines run for a total of 100 minutes, where garbage collection takes 1 minutes, Then throughput is 99%. The biggest difference between the Parallel scavenge collector and the Parnew collector is that the focus is different, and it focuses on throughput rather than the stop time of the stops. The shorter the pause time, the more suitable for the need to interact with the user program, good response speed can improve the user experience, and high response ratio can be efficient use of CPU time, as soon as possible to complete the operation of the program tasks, mainly for the background operation and do not need too many interactive tasks. Main See figure ↑ demo serial/serial old collector run. The Serial old collector is an older version of the Serial collector, which is used primarily in client mode, if it is used in server mode, respectively, and in versions prior to JDK1.5 parallel The scavenge collector is used in conjunction with or as a backup plan for the CMS collector.

Parallel Old collector

Demonstrates that the Parallel Scavenge/parallel old collector is running an older version of the Parallel, which is a Parallel scavenge collector, and Parallel The scavenge collector's mates can achieve maximum throughput for overall application ... The parallel scanvenge and parallel old collectors can be prioritized in the context of throughput and CPU resource sensitivity.

CMS collector

\

The CMS (Concurrent Mark Sweep) collector is a collector with the goal of obtaining the shortest payback time, which is mainly used on the Internet website and the B/s system server. The CMS collector is implemented based on the tag-purge algorithm, which mainly consists of four processes

  1. Initial tag (CMS initial mark)
  2. Concurrency token (CMS concurrent mark)
  3. Re-tagging (CMS remark)
  4. Concurrent Purge (CMS concurrent sweep)
initial tag and re-tagging these two steps still require the "Stop the World" initial tag

The initial tag simply marks the object that the GC roots can directly relate to and is fast.

Concurrency token

The concurrent tagging phase is the process of GC Roots tracing.

Re-tagging

The re-tagging phase is to fix the tag record of the part of the object that caused the markup to change during the concurrent tag because the user program continues to work

Disadvantages of the CMS collector:

  1. CMS Collector is very sensitive to CPU resources
  2. CMS Collector cannot handle floating garbage (floating garbage), "Concurrent Mode Failure" may appear
  3. CMS uses the tag-purge algorithm for garbage collection, the disadvantage of the algorithm itself will cause the CMS to generate a lot of space debris generation
G1 Collector

The run G1 for the G1 (garbage first) collector is a garbage collector for service-side applications. The G1 collector has the following advantages over other garbage collectors:

  1. Parallel and concurrent, G1 can take full advantage of the hardware advantages of multi-CPU, multi-core environment, using multiple CPUs (CPU or CPU core) to shorten the stop time of the world, some other collectors have to pause the Java thread to perform the GC action, The G1 collector can still have Java programs continue to execute in a concurrent manner.
  2. Generational collections, like other collectors, are still preserved in the G1 of the generational concept. Although G1 can manage the entire GC heap independently without the need for other collector mates, it can handle newly created objects in different ways and old objects that have survived the GC for a period of time to get better collection efficiency.
  3. Spatial integration, unlike the CMS's tag-purge algorithm, G1 is based on the overall implementation of the tag collation algorithm, from the local (two region) is based on the replication algorithm implementation. However, both of these algorithms mean that memory space fragmentation does not occur during the G1 run, and that it provides regular, usable memory after collection, which facilitates long running of the program and does not cause the next GC to be triggered prematurely because of the inability to find contiguous memory space.
  4. A predictable pause, which is G1 relative to the CMS of another major advantage, reduce the pause time is G1 and CMS common concern, but G1 in addition to the pursuit of low pause, but also to establish a predictable pause time model, can let the user explicitly specify a length of M milliseconds in the time fragment, The time spent on garbage collection must not exceed n milliseconds, which is almost a feature of the real-time Java (RTSJ) garbage collector. Supplement : The memory layout of the Java heap differs greatly from other collectors when using the G1 collector, which divides the entire Java heap into separate, equal-sized regions (region), although it retains the concept of the new generation and the old age, but the Cenozoic and the old are no longer physically isolated, They are all collections that are part of the region (which does not need to be contiguous).
memory allocation and recovery policy

The memory allocation of objects, in the general direction, is allocated on the heap (but can also be JIT-compiled and then split into scalar types and indirectly allocated on the stack), the object is mainly allocated to the new generation of Eden area, if the local thread allocation buffer is started, it will be assigned by thread priority on Tlab. In a few cases it may also be assigned directly in the old age, the allocation rules are not completely fixed, the details depend on which garbage collector combination is currently in use, and the settings of the memory-related parameters in the virtual machine.

  1. Objects take precedence over Eden allocations, and 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.
  2. Large objects directly into the old age, large objects (Java objects that require a lot of contiguous memory space, the most typical large object is that very long strings and arrays) for the virtual machine is a bad news, especially the Asao of large objects, in writing programs to avoid the use of large objects.
  3. Long-lived objects will enter the old age.

Virtual machines Use the idea of generational collection to manage memory, so memory recycling must be able to identify which objects should be placed in the new generation and which should be in the old age. The virtual machine defines 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, when moved to Survivo space, and the object age is set to 1, the object in the survivor area each "endure" once minor GC, age increases or decreases 1 years old, When its age increases to a certain level (by the default of 15 years), it will be promoted to the old age. The threshold for an object's promotion to the old age can be set by the virtual machine parameter.

  1. Dynamic object Age determination.
  2. Space allocation guarantee, the allocation of memory space security is like we go to bank loans, if we are good reputation, in 98% of the situation can be repaid on time, so the bank may be the default we will be able to repay the loan on time, only a person can guarantee that I can not repay the money from his account, The bank thought I was in no danger.
Supplement
    1. Parallel (Parallel): Refers to multiple garbage collection threads working in parallel, but at this point the user thread is still in a wait state.
    2. Concurrency (Concurrent): The user thread executes concurrently with the garbage collection thread (but not necessarily in parallel, possibly alternately), the user program continues to execute, and the garbage collector runs on the other CPU.
    3. Cenozoic (Minor GC): Refers to the garbage collection action occurring in the Cenozoic. Minor GC is very frequent, and general recovery speed is also relatively fast.
    4. Old age (Major gc/full GC): Refers to GC, which occurs in the old age, Major GC, often accompanied at least once minor GC. Major GC's speed is generally more than 10 times times slower than the minor GC.
The copyright belongs to the author 0xTsmon and blog Park all, welcome reprint and Commercial, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

In-depth understanding of Java Virtual Machine garbage Collection II

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.