Question

In: Computer Science

Explain the reference count garbage collection and its disadvantages. How are the disadvantages handled?

Explain the reference count garbage collection and its disadvantages. How are the disadvantages handled?

Solutions

Expert Solution

Reference counting (refcounting) GC is one of the two primary GC mechanisms widely used. The basic workings of this GC is pretty simple and based on counting the number of reference to a memory block (or object) from other blocks. Each time memory is created or a reference to the object is assigned it’s refcount is bumped up. Each time a pointer is assigned away from it the refcount is reduced. When refcount of the block goes to 0, it means that no pointer references it and hence it is un-reachable and is garbage and can be reclaimed.

Let’s assume RefCount(VOID *pMem) gives us the reference count of the object at pMem. Then the following code shows how the reference count of a specific object in memory varies with the code flow.

Object * obj1 = new Object(); // RefCount(obj1) starts at 1
Object * obj2 = obj1;         // RefCount(obj1) incremented to 2 as new reference is added
Object * obj3 = new Object(); 

obj2->SomeMethod();
obj2 = NULL;                  // RefCount(obj1) decremented to 1 as ref goes away
obj1 = obj3;                  // RefCount(obj1) decremented to 0 and can be collected

As the execution flow mutates the state of the program, the refcount of each object is updated transparently and the block is freed when refcount hits 0.

A disadvantage of reference counting is that it does not detect cycles. A cycle is two or more objects that refer to one another. Another disadvantage is the overhead of incrementing and decrementing the reference count each time. Because of these disadvantages, reference counting currently is out of favor.


Related Solutions

Explain how the reference count garbage collection works? Which limitation is removed using TBD stack and...
Explain how the reference count garbage collection works? Which limitation is removed using TBD stack and how? [25 points]
Know how blood and lab specimens should be handled after collection
Know how blood and lab specimens should be handled after collection
How do we end up creating garbage cells? Give example C++ code. What does garbage collection...
How do we end up creating garbage cells? Give example C++ code. What does garbage collection do? Be specific. Why is it good to use a reference counter in GC? Two advantages.[2] Name a way to avoid fragmentation in allocation of different sizes.
The Waste Management Company is a garbage collection company and they are reviewing a capital investment...
The Waste Management Company is a garbage collection company and they are reviewing a capital investment in a new computer system for GPS tracking. The total initial outlay for the system is $85,000 and it includes the hardware, software and the installation. The portion of the total cost related to software is $15,000. The salvage value of the system is expected to be $12,000. The company believes the use of the new system can have operational savings of $25,000 per...
Which is the correct answer a, b, or c What variables are deallocated by garbage collection?...
Which is the correct answer a, b, or c What variables are deallocated by garbage collection? a. Global Variable b. Class field c. Unreachable objects When should you call garbage collection? a. After each method b. After declaring a class c. You don't call garbage collection
Everyone knew that Bob Mcallister was a great businessman. He had taken a small garbage collection...
Everyone knew that Bob Mcallister was a great businessman. He had taken a small garbage collection company in Kentucky and built it up to be one of the largest and most profitable waste management companies in the Midwest. But when he was convicted of massive financial fraud, what surprised everyone was how crude and simple the scheme was. To keep earnings up and stock prices soaring, he and his coworkers came up with an almost foolishly simple scheme: first, they...
QUESTION 1 1.   We can solve memory leaks with        some form of garbage collection...
QUESTION 1 1.   We can solve memory leaks with        some form of garbage collection such as mark-sweep or reference counters        some form of garbage collection such as tombstones or lock-and-keys        using a tombstone or mark-sweep approach to the dangling pointer problem        using a tombstone or lock and keys approach to the dangling pointer problem QUESTION 2 1.   Pointers (and/or reference) types are necessary in modern programming languages because        they provide...
The city of Selma (like most other cities) contracts with a single garbage collection firm, Alamo...
The city of Selma (like most other cities) contracts with a single garbage collection firm, Alamo Waste Inc., which has been given an exclusive franchise to pick up trash in the city. However, Mr. Foo L. Noone has proposed an alternative scheme. He suggests that the city government should divide the city into several, much smaller areas. Then it should allow numerous small companies that would like to get into the business of collecting trash to competitively bid for garbage...
The city of Selma (like most other cities) contracts with a single garbage collection firm, Alamo...
The city of Selma (like most other cities) contracts with a single garbage collection firm, Alamo Waste Inc., which has been given an exclusive franchise to pick up trash in the city. However, Mr. Foo L. Noone has proposed an alternative scheme. He suggests that the city government should divide the city into several, much smaller areas. Then it should allow numerous small companies that would like to get into the business of collecting trash to competitively bid for garbage...
Assume your city government has been contacting with a single garbage collection firm that has been...
Assume your city government has been contacting with a single garbage collection firm that has been granted an exclusive franchise, the sole right, to pick up trash within your entire city limits. However, it has been proposed that companies be allowed to compete for business with residents on an individual basis. The city government has estimated the price residents are willing to pay for various numbers of garbage collections per month and the total cost facing the garbage collector per...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT