You are on page 1of 5

Reference counting is a form of automatic memory management where each object has a

count of the number of references to it. An object's reference count is incremented when a
reference to it is created, and decremented when a reference is destroyed. The object's
memory is reclaimed when the count reaches zero.
There are two major disadvantages to reference counting:

If two or more objects refer to each other, they can create a cycle whereby neither
will be collected as their mutual references never let their reference counts
become zero. Some garbage collection systems (like the one in CPython) using
reference counting use specific cycle-detecting algorithms to deal with this issue.

In naïve implementations, each assignment of a reference and each reference
falling out of scope often require modifications of one or more reference counters.
However, optimizations to this are described in the literature.[clarification needed] When
used in a multithreaded environment, these modifications (increment and
decrement) may need to be interlocked. This may be an expensive operation for
processors without atomic operations such as Compare-and-swap.[citation needed]

One important advantage of reference counting is that it provides deterministic garbage
collection (as opposed to tracing GC).

In computer science, garbage collection (GC) is a form of automatic memory
management. The garbage collector, or just collector, attempts to reclaim garbage, or
memory used by objects that are no longer in use by the application. Garbage collection
was invented by John McCarthy around 1959 to solve the problems of manual memory
management in lisp.
Garbage collection is often portrayed as the opposite of manual memory management,
which requires the programmer to specify which objects to deallocate and return to the
memory system. However, many systems use a combination of the two approaches, and
there are other techniques being studied (such as region inference) to solve the same
fundamental problem

The basic principles of garbage collection are:
1. Find data objects in a program that cannot be accessed in the future
2. Reclaim the resources used by those objects
By making manual memory deallocation unnecessary (and often forbidding it), garbage
collection frees the programmer from having to worry about releasing objects that are no
longer needed, which can otherwise consume a significant amount of design effort. It also

The garbage collector will almost always be closely integrated with the memory allocator. where a reference to a deallocated object is used. even though the object or data has since been deleted and the memory may now be used for other purposes. (The pointer still points to the location in memory where the object or data was. but is never actually used. In languages that provide dynamic allocation. which occur when a piece of memory is freed while there are still pointers to it.. certain categories of bugs are eliminated or substantially reduced:    Dangling pointer bugs. and most scripting languages) or effectively for practical implementation (e.[4] Garbage collectors generally can do nothing about logical memory leaks. Certain kinds of memory leaks. garbage collection is crucial to memory safety and often to the associated property of type safety. to memory exhaustion. Novice programmers sometimes believe that garbage collection makes memory leaks impossible. including ones that do not require recompilation. Double free bugs. because it prevents several classes of runtime errors. In a logical memory leak. it is far easier to implement garbage collection as part of the language's compiler and runtime system. not realizing that logical leaks are still possible. over time.. C.g. Some languages. and one of those pointers is used. leading. others. lead to storage violation errors that are extremely difficult to detect.) Many computer languages require garbage collection. either as part of the language specification (e. allow both garbage collection and manual memory management to co-exist in the same application by using separate heaps for collected and manually managed objects. but have garbage collected implementations available (e. like Ada and Modula-3.g. a region of memory is still referenced by a pointer. This can. creating a dangling pointer. [edit] Benefits Garbage collection frees the programmer from manually dealing with memory allocation and deallocation. and often does. This in turn improves the . Java. are garbage collected but allow the user to manually delete objects and also entirely disable garbage collection when speed is required.g.[citation needed] but post hoc GC systems exist. Researchers draw a distinction between "physical" and "logical" memory leaks. these are said to be garbage collected languages. Other languages were designed for use with manual memory management. in which a program fails to free memory that is no longer referenced by any variable. which occur when the program attempts to free a region of memory that is already free. formal languages like lambda calculus). like D. For example. As a result. the last pointer to a region of allocated memory is removed. In a physical memory leak. it prevents dangling pointer errors.aids programmers in their efforts to make programs more stable. In any case. C+ +). but the memory is not freed. C#..

which creates a new instance of the Integer class. Figure: Objects with reference counters. Instead. p = q. But how can we tell when no references to an object exist? A simple expedient is to keep track in each object of the total number of references to that of the language by preventing a wide class of security vulnerabilities based on over-writing memory in unexpected ways. it may be necessary to update several reference counts. Suppose p and q are both reference variables. Only a single variable. Thus. Now consider the following sequence of statements: Object p = new Integer (57). Both p and q refer to the same object. Reference Counting Garbage Collection The difficulty in garbage collection is not the actual process of collecting the garbage--it is the problem of finding the garbage in the first place. In general. the reference count field is updated by the Java virtual machine itself. An object is considered to be garbage when no references to that object exist. Object q = p. The idea is that the reference count field is not accessible to the Java program. That is. . Consider the statement Object p = new Integer (57). every time one reference variable is assigned to another. would be implemented by the Java virtual machine as follows: if (p != q) { if (p != null) --p.refCount. p. its reference count should be two. its reference count should be one. This sequence creates a single Integer instance. we add a special field to each object called a reference count . The assignment p = q. Therefore. refers to the object.

. The advantage of using reference counts is that garbage is easily identified. Object q = new Integer (99).if (p != null) ++p. the object is garbage. This increases significantly the time taken by assignment statements.release (p).refCount. the reference counts must be adjusted as above. If the reference count is zero. It is not necessary to wait until there is insufficient memory before initiating the garbage collection process. suppose we assign q to p using the code sequence given above. Typically. Figure: Reference counts before and after the assignment p = q. p = q. As shown in Figure (a). Second. this means an extra word of storage must be allocated in each object. the garbage collector needs only to examine the reference count fields of all the objects that have been created by the program. The costs of using reference counts are twofold: First. every object requires the special reference count field. When it becomes necessary to reclaim the storage from unused objects.refCount == 0) heap. And the reference count on Integer(57) has gone to zero which indicates that it is garbage. Now. Figure (b) shows that after the assignment. each with a reference count of one. two Integer objects are created. every time one reference is assigned to another. We can reclaim memory used by an object immediately when its reference goes to zero. Consider what happens if we implement the Java assignment p = q in the Java virtual machine as follows: if (p != q) { if (p != null) if (--p. } For example suppose p and q are initialized as follows: Object p = new Integer (57). both p and q refer to the same object--its reference count is two.

. when it becomes garbage.e. } Notice that the release method is invoked immediately when the reference count of an object goes to zero. In this way. garbage may be collected incrementally as it is created..refCount. i.if (p != null) ++p.