JVM Series II: Garbage Collection

When recycling target

Reference counting 

1, Principle: adding a reference count for the object, when the object increases a reference count is incremented, the failure counter is decremented when a reference. Reference count for the object 0 may be recovered.

2. Disadvantages: can not solve the problem of circular references

Reachability analysis

1, Principle: the GC Roots as a starting point to search, reachable objects are alive, unreachable objects can be recovered.

2, can be used as the target GC Root:

  • VM stack objects referenced in the local variable table
  • JNI native method stack objects referenced
  • Method static property class object referenced by region
  • Method zone object literal references

Area recycling method

1, mainly unloading and the recovery of the class of constant pool for the next scene draws heavily on dynamic proxies and reflection, uninstall class is of great significance

2, class unloading must meet the following three conditions, but even if three conditions are met does not necessarily be uninstalled

  • All instances of this class have been recovered, then the stack of any instance of the class does not exist.
  • Load class ClassLoader has been recovered.
  • Class object corresponding to the class is not referenced anywhere, you can not access the class through reflection method anywhere.

finalize method

1, similar to the C ++ destructor for closing the external resources. But try-finally, etc. can do better, and the method is very high running costs, uncertainty large, can not guarantee the calling sequence of each object, it is best not to use.

2, when an object can be recovered, if you need to perform the object's finalize () method, then it is possible to re-let the object is referenced in this method, in order to achieve self-help.

Self-help only once, if called before the object recovered finalize () method of self-help, and will not call the method behind recycling

Four kinds of reference

Whether or reference counting reachability analysis, judgment and cited the count is very important

Strong references

1, features: will not be recovered

2, the structure of the way: new

 1 Object object = new Object(); 

Soft Application

1, features: will only be recovered in time is not enough memory

2, constructed way: SoftReference

. 1 Object Object = new new Object ();
 2 the SoftReference <Object> = SF new new the SoftReference <Object> (Object);
 . 3 Object = null ; // make the object can only be associated with soft references

 

Weak references

1, features: next memory recovery time will be recovered

2, constructed way: WeakReference

1 Object object = new Object();
2 WeakReference<Object> sf = new WeakReference<Object>(object);
3 object = null;

 

False quote

1, features: no way to get a phantom reference object, set its sole purpose is to get a notification when it's recycling system

2, constructed way: PhantomReference

1 Object object = new Object();
2 PhantomReference<Object> sf = new PhantomReference<Object>(object);
3 object = null;

 

Garbage collection algorithm

Mark - sweep algorithm

1, process

(1) marking phase: the program checks whether each object to active object, if the object is active, the program for marking at the head of the subject.

(2) removal stage: the object will be recovered and cancel flag, Further, the recovery will be determined after a previous block if a continuous free block, if continuous, will merge the two sub-blocks.

The object is to be recovered as a block, connected to the one-way linked list called the "free list", just after the time to traverse the free list allocation, the block can be found.

When dispensing, the program searches the free block list to find space block size greater than or equal to the size of the new object. If it finds a block equal size, returned directly to the block;

If the block size is greater than found, it will be divided into blocks of size and size (block - size) two-part, return the size of the block size, and the size (block - size) is returned to the free block list.

2, illustrated

 

 

3, shortcomings

  • Marking and clearance process efficiency is not high
  • May produce large amounts of memory fragmentation

 

Mark - Collation Algorithm

1, process

Let all surviving objects are moved to the end, then clean out the memory directly outside the terminal boundary.

2, illustrated

 

 

3, shortcomings

Every object moves have survived

 

Replication algorithm

1, process

The memory is divided into two equal size, wherein one uses only when it runs out of memory a copy of the object will still alive one to another above and then used to conduct a clean memory.

Now commercial virtual machines are using this new generation of recycling collection algorithm, but it is not divided into two equal size, but a large space of Eden and two Survivor smaller space, Eden and which one each use Survivor. When recycling, the Eden and Survivor also alive objects copied to another piece of Survivor, final clean-up Eden and used it a Survivor.

Eden and Survivor size ratio HotSpot virtual machine defaults to 8: 1, to ensure that the memory utilization rate of 90%. If every object is more than 10% recovery of survival, then a Survivor is not good enough, then you need to rely on old's spatial distribution guarantee, which is stored in the object space does not fit the old era borrowing.

2, illustrated

 

3, shortcomings

You can only use local memory

 

Generational collection algorithm

1, in accordance with the object lifetime memory divided into different zones is divided into the old and the new generation's, different regions adopt different collection algorithms

2, the new generation: Take replication algorithm

   Old year: take mark - cleaning algorithm or mark - Collation Algorithm

 

The garbage collector

 

The new generation of collectors

Serial Collector

 

ParNew collector

 

Parallel Scavenge collector

 

And ParNew as multithreaded collector.

Other collectors goal is to shorten the pause time garbage collector thread of the user as much as possible, and its goal is to achieve a throughput can be controlled, so it is called "throughput priority" collector. Here refers to the ratio of the throughput of CPU time for the total operation time of the user program.

 

Collector years old

 

Serial Old collectors

 

Parallel Old collectors

 

CMS collector

 

G1 collector

 

 

Collocation

 

Compare summary

 

collector Collection algorithm New Generation / years old Remark
Serial Replication algorithm Cenozoic
ParNew Replication algorithm Cenozoic
Parallel Scavenge Replication algorithm Cenozoic  
Serial Old Mark - Collation Algorithm Years old  
Parallel Old Mark - Collation Algorithm Years old
CMS Mark - sweep algorithm Years old  
G1 Mark - Collation Algorithm + Years old Cenozoic  

Guess you like

Origin www.cnblogs.com/huanglf714/p/11027336.html