GC common algorithm

What is garbage collection
program run will produce a variety of all kinds of data, then the data is in memory of them, these data can not be permanent, invalid resource objects need to be garbage collected, freeing memory.

 

Reference calculation method

This object has a reference, not only increasing a counter, a reduction delete a reference counter, when garbage collection, recycling only counter 0 objects.

Features: simple but very slow, can not handle the case of a defect is circular references.

 

Clear labeling law

Tags: starting from the root object reference marks
  clear: the object is garbage unmarked objects, you can clear

  principle: Mark all objects in the initial state are zero, when out of memory,
  marking phase: GC will be marked first stage, starting from the root, all object references are marked as 1, mark no other objects referenced to 0
  after all objects labeled 1 represents the referenced object exists is not cleared, the object is marked as 0 Clear, Clear finish: Clear stage All objects relabeled 0

Disadvantages:

  1. low efficiency, mark and remove the two movements are required to traverse all the objects, and in the GC, you need to stop the application, for the more demanding interactive applications that experience is very poor.
  2. The fragmentation is more serious and may lead to large data there is no way to store small data storage space wasted

 

Replication algorithm

In order to address efficiency issues, it would in accordance with the available memory capacity is divided into two pieces of equal size, uses only one of them. When this piece of memory is used up, it will be copied to another piece of living objects above, then put the memory space has been used once cleared away. So that every time zone for the entire half-memory recovery, also do not consider the complexities of memory chips and other memory allocation. Long as the mobile heap pointer, in order to allocate memory, simple, efficient operation.

Defects: The cost of this algorithm is to reduce memory by half.

 

Generational Algorithms

Suitable garbage collection objects follow the appropriate algorithm, JVM among the young generation for replication algorithm, the old generation can remove hair mark or marks compression algorithm

Guess you like

Origin www.cnblogs.com/cw172/p/12424261.html