GC algorithms - Reference counting

Outline

Reference counting what the hell is it? As the name suggests, a reference to the object count the number of times cited by recording each object to determine whether the object can be recycled.

achieve

First, the target number of references to manage, when it will update it?

  1. Create an object: create a new object (reference number +1 for this new object)
  2. Update pointer: a pointer to an object point A to point B again objects (number of object references to A -1, B object reference number 1)

This is not the code, a brief idea on the line. (My brother said, looking at the code strenuous)

Premise : We have a global free address list: FREE_HEAD

That create objects

  1. Looking memory from the FREE_HEAD
  2. If found, the object counter is set to 1, return
  3. If not found, memory expansion, returns 1

Update pointer operation

  1. The new object reference count +1
  2. The old object reference count -1 -1 if the reference number is 0, then add the object and all child objects to FREE_HEADthe list.

Speaking achieve simple, after all, I do not really go to achieve, simply think about.

analysis

In the last 标记清除算法in, GC runs every time out of memory, will inevitably lead to suspension of the program a long time, but 引用计数it is managed at the same time each pointer changes, recovered immediately when a new garbage. This reflects out of its several advantages:

  1. The maximum short pause.
  2. Garbage can produce immediate recovery

Of course, just say do not say the advantages and disadvantages are pulled calf. First of all, 引用计数the advantage will be its disadvantage, frequently count calculation, the speed will be dragged down by the program. Each object and must open up space to store a reference number. Of course there often comes a circular reference problem. and so on it.

  1. Frequent updates the reference count drag speed program
  2. Each object needs to open up extra space for your reference count
  3. Circular reference object can not be recycled (A reference is to B, B references A. But they are not referenced by other objects, resulting in a reference They always 1, can not be recycled)

Of course, for the problem, there is always a great way to solve the previous example:

Delay counting : For frequent updates counter issues raised about the meaning is not a reference to the number of real-time update, the reference number for the record to be processed in a linked list 0 when the need for new memory reunification process. However, this will increase the pause time before they do.

Sticky Reference counting : reference count by the number of references to save additional space, but that there must be a maximum value, for example one byte, the reference number no less than 256 on the mind of this method of treatment is out of range. simple, do nothing, do not recover, after all, been cited so many times, the object will certainly very important that these objects will not never be recovered it? Yes, wait until there is no memory, and using 标记清除算法all objects over again .

Of course, there are many methods for reference counting evolution, some of which are very interesting, some I do not understand.


Garbage collection as a whole is divided into two schools of thought (I know):

  1. Reference counting: that is to say above
  2. Accessibility: that is 标记清除the kind of determine whether an object can be reached.

引用计数The biggest advantage is that it should not need to pause the program to be recovered, with recovery but with the use of inferior also obvious: the need for extra counter space and a circular references.

Personally I prefer 引用计数the real-time high, and does not require much extra space just need to deliberately avoid when writing code in a circular reference, or other methods to circumvent it? Do not even deal with all deliberate, if only a few words ( If there are many, it is still another algorithm).

Guess you like

Origin www.cnblogs.com/hujingnb/p/12638443.html