Reference Counting

If each object contains a counter that tracks the number of references to that object, the system can free an object as soon as the reference counter goes to zero. This forces every operation that creates or deletes a reference to an object to maintain the reference count, but it amortizes the cost of garbage collection over all those operations and it frees memory at the earliest possible moment. Garbage collection by reference counting is simple and reliable, except for one problem.

Reference counting cannot easily detect cycles. If A contains a reference to B, and B contains a reference to A, they both have a reference count of one. The garbage collector will not be able to free them even though there is no way to reach ...

Get Real-Time Java™ Platform Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.