Home Technique Quote count

Quote count



Introduction

The most straightforward garbage collection strategy is reference counting. Reference counting is very simple, but it is important to meet the needs of the compiler, and increases the cost assignment function (mutator) of (this term is for the user program, the garbage collector from the point of view of). Each object has an associated reference count - the number of active references to the object. If the object's reference count is zero, then it is rubbish (user program can not reach it), and can be recycled. Modifying pointers each time reference (such as by an assignment statement), or when a reference out of range, the compiler must generate code to update the reference object's reference count. If the object reference count becomes zero, it can be run immediately recover this block (reference count of all blocks and the block is recovered to reduce the reference), or to collect it on a queue delay.

com component will maintain a reference count value is referred to. When the client acquired from an interface component, this will increase a reference count. When finished using the client an interface, the reference count module Save 1. When the reference count value is 0, the component can be deleted from the own memory.

using reference counting

Why

reason

To choose to maintain a separate reference to each count of the interface rather than the reference count for the entire assembly to maintain it?

There are two main reasons: First, make debugging easier; Another reason is that demand access to support resources.

1 debugging:

assumptions forget a call to Release interfaces (in fact, many people make this mistake) in the program. Such components will never be removed, because it is only in reference count 0:00 delete will be called. Then you need to find out when and where the interface should be released. Of course, it is very difficult to find. In the case of only one interface to maintain the entire assembly for such a result is more difficult to find. At this time, all interfaces must be checked using a code provided by this component. However, if the components are supported for each interface maintains a reference count you can find a range of restrictions on a particular interface. In some cases this can save a lot of time.

2. Obtain needed resources

may require a lot of memory or other resources when implementing an interface. For this case, can complete the allocation of resources when the client requests this interface in the implementation of QueryInterface. However, if a reference count only maintain the entire assembly, components can not determine when it is safe to release some of this memory associated with the interface. But a base reference count maintained separately for each interface, you can decide when to release this memory will be much easier.

Guize

proper use reference counting three simple rules

1. Tiaoyong AddRef before returning. For those functions that build better returns an interface pointer before returning corresponding pointer should call AddRef. These functions include QueryInterface and CreateInstance. So that when the client obtained from such an interface function. It will not need to call AddRef.

2. After using the interface after calling Release. After using an interface should be called something Release function interface.

3. Call AddRef after the assignment. When an interface pointer assigned to another interface pointer should call AddRef. In other words, after establishing a reference outside the interface should not increase the reference count of the corresponding component.

Interface

in the client view, the reference count is at the interface level, rather than the component level. But from an implementation perspective, whose reference count is recorded virtually no relationship. Customers can believe that a direct component will record each interface itself maintains a reference count. But the client can not assume that the entire assembly to maintain a single reference count.

For customers, each interface is a separately maintained reference count means that customers should call AddRef for the pointer will be used instead of any other pointer. For pointer over customers should call their Release.

Quote count

choose to maintain a separate reference to each count of the interface rather than a reference count of the reasons for the maintenance of the entire assembly:

make debugging easier; demand access to support resources; < / p>

commissioning

may be achieved by increasing and decreasing the value of a number.

Also note that AddRef and Release the return value does not make sense, but it may come in handy in debugging the program. Customers should not from this value as this is the precise reference to the component or interface number.

rules

customer interfaces must be processed as a reference count for each interface has a separate. Therefore, customers must reference count separately for different interfaces, even though their life span is nested.

a, the output parameter rule

means that the output parameters to the caller of the function return value of a function parameter. From one o'clock in terms of effect, the return value of the output parameter is similar to the same function. Any output parameters returned as a return value or a pointer to the new interface of some function must be AddRer interface pointers.

Second, the input parameters of the rule

interface pointer passed to the function, without calling AddRef and Release, this is because the life of the nested function in the caller's lifetime.

Third, the input - output parameter rule

Input - Output parameters both input parameters and output parameters of the function. Function may be used in the rest of the input - output parameter values, then these values ​​may be modified and returns it to the caller.

in the function for input - output parameters passed in interface pointer, it must call Release prior to assign it to another interface pointer value. Before the function returns, you must call AddRef output parameters stored in the interface pointer.

Fourth, local variables rules

For topical homemade interface pointer, but only because of their presence in the life of its function, and therefore no need to call AddRef and Release. This rule is actually a direct result of the input parameters of the rule. In the following example, pIX2 only in the function foo lifetime, thereby ensuring its lifetime will be nested within the lifetime of the incoming pIX pointer, there is no need to call AddRef and Release of pIX2.

five, global variables rules

to the interface pointer stored in the global variable, before it is passed to another function, it must call AddRef. Because this variable is global, so any function can be used to terminate their lifetime by calling its Release. For the interface pointer stored in the member variable, it should also be treated this way. Because any member functions of this class are the state of the interface pointer may be changed.

Six Rules

When can not be determined for any uncertain situation, should call AddRef and Release right.

In addition, when determining to be optimized, should be no pointers to those of the reference count with the corresponding comments, or other programmers in modifying code, it may increase the life of an interface pointer , thereby closing the reference count optimization destroyed.

Error forget to call Release cause may result more difficult to detect than not call AddRef. The main advantage compared with the tracking Garbage collection

advantages and defects

, the reference count of the object is no longer used can be recovered as soon as possible, at the same time does not result in the recovery process long pause, also clearly marked the life cycle of each object.

in real-time applications or memory-constrained systems, real-time responsiveness is an important indicator, and the reference count is one of the easiest to implement garbage collection, it is suitable to the case. Reference count may also be used to manage other non-memory resources, such as the operating system object (often more scarce than memory resources). Garbage collection tracking technology to deal with such a target with a terminator, but delayed the recovery may lead to other problems. Weighting is applied to the reference count derived art distributed systems.

on the available memory is filled with active target platform, tracking garbage collection will be triggered frequently, thereby reducing performance. The reference count even in the case of memory on the verge of exhaustion performance still be safeguarded. Reference count also for other optimization techniques to provide run-time reference information, for example, many systems for immutable object (e.g., functional programming language), a large number of duplicate object may result in serious performance penalty; on such a system a typical optimization measures are: the future if an object is created only once, and another similar object is created at the same time it is no longer referenced out (such as string concatenation assignment operator in Javascript), you can delete the original object creates a new object's behavior becomes modify the original object, thus improving efficiency. Reference count may provide sufficient reference information for such optimization.

optimized reference count without trace compared Garbage collection has two major drawbacks, we need to introduce additional mechanisms to repair:

  • frequently updated reference count It will reduce the operating efficiency.

  • original reference count circular reference problem can not be solved. Spatial locality

Further, if a free list memory allocation, then the reference count is very poor. Using only the reference count CPU cache to improve performance by moving objects, high-performance memory allocator will implement both a tracking garbage collector to improve performance. Many reference count implementation (such as PHP and Objective-C) poor performance because they did not realize memory copy.

This article is from the network, does not represent the position of this station. Please indicate the origin of reprint
TOP