GC garbage collection

The objects of GC in java are heap space and permanent area. The

old garbage collection algorithm: garbage is collected by reference counting. As long as one person uses it, add 1 to the object, and if someone releases the object, decrement it by 1. If the reference count is 0, Then the
object A will be recycled
Reference counting problem: (not adopted by java)
reference and dereference are accompanied by addition and subtraction, which affects performance
It is difficult to deal with circular references, that is, A references B, B references C, C references A, and the loop is closed .


Mark-Clear:

It is the ideological basis of modern garbage collection algorithms. Garbage collection is divided into two stages, as in the title; in the marking stage, the root node is first passed to mark all reachable objects starting from the root node, so the unmarked objects are unreferenced Garbage objects, and then the cleanup phase removes all unmarked objects.


markup-compression:

It is suitable for occasions where there are many surviving objects, such as the old age, starting from the follower node, marking all reachable objects once, but then, not simply clearing the unmarked objects, but all surviving objects. Objects are compressed to one end of memory, after which all space outside the bounds is cleaned up.


Replication algorithm:
not suitable for occasions with many surviving objects, such as the old age,
divide the original memory space into 2 blocks, and use only 1 block at a time. During garbage collection, the surviving objects in the memory being used will be Copy to the unused memory block, after that, clear all objects in the memory block in use, swap the roles of the 2 memory, and complete the garbage collection.

Cons: Waste of space





Generational thinking: Classify objects according to their life cycles. Short-lived objects belong to the new generation, and long-lived objects belong to the old generation.
According to the characteristics, select the appropriate collection algorithm:
a small number of objects survive, suitable for the replication algorithm;
a large number of objects survive, suitable for mark cleaning or mark compression.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324591997&siteId=291194637