jvm study five: Garbage Collection

There is a wall of dynamic memory allocation and garbage collection techniques surrounded by the "wall" between Java and C ++, people want to go outside of the wall, the wall, but the people inside want out.

Compared with the development of C and C ++, java programmers are relatively happy, because they no longer need to consider the application and recovery of memory, all memory management all work related to the JVM, developers will naturally be a lot easier, at least a memory leak problem It will be greatly reduced. But everything has pros and cons, of course, automatic memory management simplifies the development, but also let programmers lost some ability to control the program, once the problems with the program, and this is precisely because of this problem out of control parts produced, then the problem will be very difficult even impossible to start the investigation, after all, the garbage collector for java programmers is a black box. To avoid this situation, it is necessary to learn the JVM memory management.

The festival said jvm memory composition and division, this section will talk about the jvm garbage collection related to memory. jvm garbage collection occurs mainly in the heap, although there are also other areas reclaim memory, but is rarely used, and similar ideas and algorithms, so as to recover the memory of learning objects

Memory generational concept

  Memory recovery is a troublesome thing to deal with bad great influence on the performance of the program, according to people discover, when memory reclamation Depending on whom the survival period of the memory is divided into parts, sub-block is a relatively good recovery strategy. So the development of the garbage collector manufacturers have adopted this strategy generational memory. jvm memory is generally divided into two parts, the new generation (Young Generation) and the old year (Old Generation) (For Hot Spots virtual machine there is a permanent generations, in fact, it is the way area, but because of the realization there is a problem, then abandoned a), the new generation of programs stored in the newly created object, save the old program's are long-standing objects.

Note: The concept of the new generation and there Eden (Eden Spac) and survival zone (Survivor Space), followed by the memory reclamation algorithm will be mentioned 

 

Garbage collection algorithm  

  The impact is relatively large memory recovery program performance, good or bad memory recovery algorithm is very important, after decades of development, memory recovery produced a variety of algorithms, each algorithm is different, adapt to different scenarios, introduced one by one below

1) generational recovery: memory Generational algorithm is the realization of memory generational recovery of thinking, it is a different lifecycle objects are managed separately, generational recovery characteristics of different generations objects, recovery time, the policy can be different. It looks very simple, but this method has been used almost all kinds of garbage collectors.

2) mark - sweep algorithm: the most basic collection algorithm, see the name to know Italian, this algorithm is to heap objects have failed to mark out a unified memory after the completion of recovery of these objects. This algorithm is the simplest method, but it is the basis for memory recovery algorithm, other algorithms are extended and optimized on the basis of

Advantages: simple

Cons: mark and clear process speed is not high, affecting the efficiency of the program; will generate a lot of memory fragmentation after memory recall, resulting in the allocation of large objects in memory in the future, but obviously there are a lot of available memory can not be used 

3) Copy the algorithm: the memory is divided into two, which each use one, when one of a run out, once garbage collection, copy objects that survive to another piece, then that piece of memory used before all clean out.

Advantages: simple, no memory fragmentation, there is no need to consider how to deal with memory fragmentation

Disadvantages: memory utilization is low, only 50% of the memory; when an object higher survival rate larger than the data needs to be replicated, the efficiency is not high

Supplementary: IBM Research shows that 98% of the program in object life cycle is very short, according to a 1: 1 ratio of division is a waste of space, so manufacturers the door door with a new strategy, the memory is divided into a larger piece and the Eden space Survivor two small space, the ratio is generally Eden: Survivor1: Survivor = 8: 1: 1. Eden and which each use a Survivor, when recovered, the Eden and Survivor also alive objects once copied to another piece of Survivor space, and finally clean out Survivor Eden and just used space. Now commercial virtual machines are using this collection algorithm to recover the new generation.

While such a strategy to improve the memory utilization, but may cause problems Survivor memory is not enough, so we need extra memory as a backup, this extra memory is tenured. When the object Survivor insufficient memory to store still alive, these objects directly into the tenured inch, which is "guaranteed allocation"

4) mark - sorting algorithms: an algorithm for garbage collection years old, because the old year did not want to directly use the copy algorithm to waste 50% of memory space, there is no longer a guarantee additional memory, it appeared that the new algorithm. This algorithm combines the mark - sweep algorithm and the replication algorithm, each time garbage collection object is still using the first marker, and then copy the object to one side of the memory block years old, then clear all the memory on the other side.

Advantages: simple, no memory fragmentation; memory utilization is relatively high

Disadvantages: need to mark and copy, replication efficiency is not high algorithm

 

The garbage collector

1) Serial collectors: the most basic and primitive garbage collector, the new generation garbage collection, single-threaded collector

Advantages: simple and efficient, the number of threads and processors cpu relatively few cases, the efficiency of this collector may be more efficient than the other of the collector

Cons: garbage collection when the user thread will suspend all (Stop The World), affecting the user experience; not a good use of multi-cpu, multi-threaded environment

Multi-threaded version of the Serial collector: 2) ParNew collector

Advantages: better use of multi-threading; able to work with CMS collector

Disadvantages: the presence of Stop The World; presence thread switch, the number of threads and processors cpu relatively small, the efficiency is not high

3) Parallel Scavenge: multithreading collector, and ParNew is very similar. But it provides a special function, you can control the pause time garbage collection, improve the user experience

Advantages: better use of multi-threading; it can control the pause time garbage collection, improve program throughput

Disadvantages: more complicated, the actual results may not achieve the intended user, Stop The World reducing the time required to reduce the size of the new generation of memory, so each time garbage collection is fewer, but has recovered frequency becomes higher, so we know the specific circumstances need to be tested

4) Serial Old: old's version of the Serial collector, collectors single-threaded

Advantages: simple and efficient, the number of threads and processors cpu relatively few cases, the efficiency of this collector may be more efficient than the other of the collector

Cons: garbage collection when the user thread will suspend all (Stop The World), affecting the user experience; not a good use of multi-cpu, multi-threaded environment

5) Parallel Old: Parallel Scavenge collector's version of the old, the use of multi-threading and "mark - finishing" algorithm

Advantages: better use of multi-threading; it can control the pause time garbage collection, improve program throughput

Disadvantages: Similar to the Parallel Scavenge

6) CMS collector: concurrent low pause collector is a pause for the shortest recovery time objectives collector, this part of the process of garbage collection and the collector can be user thread concurrency. Stop The World maximize reduce time

Advantages: EC pause shortest time, improve the user experience

Disadvantages: Because garbage collection and user threads are concurrent, will inevitably lead to the user program to reduce the number of threads available, on the number of threads itself is relatively small server, the performance impact is very serious; CMS achieved based on the "mark - sweep" algorithm will produce memory fragmentation

All good design for a garbage collector server applications, integrate almost more than the garbage collector: 7) G1 collector

Advantages: multi-threaded, full use of the advantages of server hardware, shorten the time Stop The World, while the user can perform concurrent threads; based on the "mark - finishing" algorithm, no memory fragmentation; can be set to pause time and improve system throughput

Disadvantages: it appears relatively late, without much commercial experiments, specific performance can not say

 

Guess you like

Origin www.cnblogs.com/fanjie/p/11294722.html