JVM garbage collection algorithm -GC

《深入理解Java虚拟机 第3章》

1, mark - sweep algorithm

The most basic collection algorithm, mark and sweep into two stages: first mark all objects need to be recovered, all unified object is marked recovery after labeling is complete.

Subsequent algorithms are collected, and insufficient improvement obtained based on this idea.

There are two main problems
1, efficiency, marking and removal efficiency of the two processes is not high
2, space issues, will generate a lot of discrete memory fragmentation mark after clearing space debris could cause too much time in the future need to allocate large objects in the program is running, can not find enough contiguous memory and had to trigger another advance garbage collection action.

2, replication algorithm

To understand the efficiency problems arise. Available memory capacity is divided by two equal in size, uses only one of them. When this piece of memory runs out, the copy will also survive object to another piece of the above, then memory space has been used once and then clean out. Such that for every half-area of the entire memory recall, also do not consider the complexities of memory fragmentation isochronous memory allocation, as long as the top of the stack pointer movement, in order to allocate memory, simple, efficient operation. The cost of this algorithm is to reduce the memory to half of the original, would be a bit too high.

Now commercial virtual machines are using this collection algorithm to recover the new generation.

The company specializes in IBM showed that 98% of the new generation of the object is toward evening students dead, it does not require a 1: 1 ratio to divide the memory space, but the memory is divided into a larger space and two Eden Survivor smaller space, and each time Eden where a Survivor.

When recycling, the Eden and Survivor also alive objects one-off piece of Survivor copied to another space, and finally clean out Survivor Eden and just used space.

HotSpot VM default size and Survivor Eden ratio is 8: 1, i.e. each new generation of memory space available to 90% of the entire capacity of the new generation, only 10% of the memory will be wasted.

Of course, only 98% of the recoverable data objects in a scene in general, can not guarantee that each have only recovered more than 10% of live objects, when Survivor space is not enough, need to rely on other memory (referred to herein as old years) were assigned guarantee .

Allocation guarantee is this: If the other one did not survive Survivor space objects on the new generation of enough space to store a collection down, these objects will go directly to the old year by allocating guarantee mechanism.

3, Mark - Collation Algorithm

Copying collection algorithm will be carried out more replication objects at higher survival rate, efficiency will be low. If you do not want to waste 50% of the space, but also require additional space allocation guarantees to cope with extreme situations all have 100 percent survival of memory is in use, it is generally in older years can not directly choose this algorithm.

According to old's characteristics, it was suggested that the "mark - finishing" algorithm.

The labeling process and "mark - sweep" algorithm is the same, but the subsequent steps are not directly recycled objects to clean up, but to all surviving objects are moved to the end, then clean out the memory directly outside the terminal boundary.

4, generational collection algorithm

The current garbage collection business virtual machine implements a "generational collection" algorithm.

Depending on the object alive the memory cycle is divided into a few pieces. The Java heap is generally divided into the old and the new generation's, so you can use the most appropriate collection method based on the characteristics of each era.

In the new generation, we have found a large number of objects every garbage died, only a few survive, then choose the collection replication algorithm , only need to pay a small amount of live objects replication costs to complete the collection.

In the old era because of the high survival rate of the object, there is no additional space for him to be secured, it is necessary to use the "mark - clean-up" or "mark - finishing" algorithm to recover.

Cenozoic Years old
Replication algorithm Mark - to clean up or mark - finishing

The garbage collector

If the algorithm is a collection methodology memory recovery, then the garbage collector is to recover memory of implementation.

  1. Serial Collector

    Replication algorithm

Stop The World!

When single-threaded collectors, garbage collection, the work must suspend all other thread until the end of his collection.

advantage:

Simple and efficient for running virtual machines in Client mode is a good choice

  1. ParNew collector

    Replication algorithm

In fact, multi-threaded version of the Serial collectors, in addition to the use of multiple threads for garbage collection, the other acts include all control parameters Serial Collector available collection algorithms, Stop The World, object allocation rules, and so the recovery strategy and Serial collector exactly the same, also in achieving considerable share code.

advantage:

Virtual machines running under Server mode of choice for the new generation of collectors, one important reason is that, in addition to Serial collector, it currently only works with the CMS collector.

  1. Parallel Scavenge collector

    Replication algorithm

The new generation of collectors, but also use the copy algorithm collectors, but also a parallel, multi-threaded collector. . .

And various other points collector

Other collectors focus is to shorten the pause time garbage collection thread as much as possible from time to time users

Parallel Scavenge object collector are: to achieve a certain controlled.

Is called a certain ratio of the total elapsed time for the CPU time and the CPU is running user code, i.e., a certain time to run user code = / (time to run user code + garbage collection time).

It provides two parameters for precise control of certain

parameter Explanation
-XX:MaxGCPauseMillis Control the largest garbage collection pause time
-XX:GCTimeRatio Throughput size disposed directly
  1. Serial Old collectors

    Mark - Collation Algorithm

Serial old collector's edition, single-threaded

The main significance is that the virtual machine to use in the Client mode

Two purposes under the Server mode
1, the JDK 1.5 and earlier in the collector with the use of Parallel Scavenge
2, as a back-up plan CMS collector is used in the Concurrent Mode Failure concurrent collection happen.
  1. Parallel Old collectors

    Mark - Collation Algorithm

Parallel Scavenge collector's version of the old, multi-threaded, began in the JDK 1.6 provides

Focus on throughput and CPU resources sensitive applications, can give priority to add Parallel Old Parallel Scavenge collector.

  1. CMS collector

    Full name: Concurrent Mark Sweep

Mark - sweep algorithm

A kind of pause obtain the shortest recovery time objectives collector.

Apply to Internet sites on the server side or B / S system, pay attention to the response speed of the service, the shortest want the system to pause, to give users a better experience.

Four steps

  1. The initial mark
  2. Concurrent mark
  3. Relabel
  4. Concurrent Clear

advantage:

Concurrent collection, low pause

Disadvantages:

  1. Very sensitive to CPU resources
  2. We can not handle floating garbage

    With the new program will continue to run the garbage produced, never clean up finish.

  3. Based on the "mark - sweep" algorithm, there will be plenty of space debris

  1. G1 collector

    Garbage-First one of the most cutting-edge technology to collect the fruits of development today when collectors

The garbage collector is designed for server applications

Features:

  1. Parallel and Concurrent
  2. Generational collection
  3. Spatial integration
  4. Predictable pause

Guess you like

Origin www.cnblogs.com/ffeiyang/p/12171474.html