25

What are the differences between shared pointers (such as boost::shared_ptr or the new std::shared_ptr) and garbage collection methods (such as those implemented in Java or C#)? The way I understand it, shared pointers keep track of how many times variables points to the resource and will automatically destruct the resource when the count reaches zero. However, my understanding is that the garbage collector also manages memory resources, but requires additional resources to determine if an object is still being referred to and doesn't necessarily destruct the resource immediately.

Am I correct in my assumptions, and are there any other differences between using garbage collectors and shared pointers? Also, why would anyone ever used a garbage collector over a shared pointer if they perform similar tasks but with varying performance figures?

helloworld922
  • 10,801
  • 5
  • 48
  • 85
  • 1
    Using shared pointers **is** a garbage collection technique. Many garbage collectors implement, at least as a first step, reference counting. – Karl Knechtel Jan 11 '11 at 22:29

4 Answers4

20

The main difference lies, as you noted, in when the resource is released/destroyed.

One advantage where a GC might come in handy is if you have resources that take a long time to be released. For a short program lifetime, it might be nice to leave the resources dangling and have them cleaned up in the end. If resource limits are reached, then the GC can act to release some of them. Shared pointers, on the other hand, release their resources as soon as the reference count hits zero. This could be costly for frequent acquisition-release cycles of a resource with costly time requirements.

On the other hand, in some garbage collection implementations, garbage collection requires that the whole program pause its execution while memory is examined, moved around, and freed. There are smarter implementations, but none are perfect.

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
  • 4
    There are pauseless, incremental and concurrent garbage collectors that do not require the mutator to be interrupted by the collector. Also, as the literature shows, garbage collectors generally perform better than reference counting in the typical mutator. – james woodyatt Jan 12 '11 at 01:02
18

Those Shared Pointers (usually called reference counting) run the risk of cycles.

Garbage collection (Mark and Sweep) does not have this problem.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    could you explain what you mean by "risk of cycles"? Do you mean having the reference count "wrap around" because of the data type used to hold the reference count? Or something else? – helloworld922 Jan 11 '11 at 22:59
  • 1
    Ah, I see. Thanks. Too bad stack overflow doesn't let you flag multiple good answers :( I think both of these are great answers – helloworld922 Jan 11 '11 at 23:08
  • Thats why good shared pointers implementations also implement something like weak pointers which does not participate in the reference count thus not "owning" the resource. – flazzari Jan 28 '15 at 12:50
1

In a simple garbage-collected system, nobody will hold a direct pointer to any object; instead, code will hold references to table entries which point to objects on the heap. Each object on the heap will store its size (meaning all heap objects will form a singly-linked list) and a back-reference to the object in the object table which holds it (or at least used to).

When either the heap or the object table gets full, the system will set a "delete me" flag on every object in the table. It will examine every object it knows about and, if its "delete flag" was set, unset it and add all the objects it knows about to the list of objects to be examined. Once that is done, any object whose "delete me" flag is still set can be deleted.

Once that is done, the system will start at the beginning of the heap, take each object stored there, and see if its object reference still points to it. If so, it will copy that object to the beginning of the heap, or just past the end of the last copied object; otherwise the object will be skipped (and will likely be overwritten when other objects are copied).

supercat
  • 77,689
  • 9
  • 166
  • 211
0

In languages with a garbage collector (GC), the GC keeps track of and cleans up memory that isn’t being used anymore, and we don’t need to think about it. In most languages without a GC, it’s our responsibility to identify when memory is no longer being used and to call code to explicitly free it, just as we did to request it.

more details: HERE

Ayoub Boumzebra
  • 3,885
  • 3
  • 21
  • 37