• Advertisement
Sign in to follow this  

Why Need A Garbage Collector?

This topic is 4715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, Over the past day i have been playing with memory management. First i overloaded new and delete to see what goes on. Then secondly i wrote a singleton class that keeps track of the allocated data, and the total allocated etc. No i here all this jazz about garbage collectors and superpig went over one in Enginuity but i still can't honestly see the point. Now i know im wrong, not trying to be naive here, but i have never had it explained to me properly. Would anyone mind explaining? ace

Share this post


Link to post
Share on other sites
Advertisement
The garbage collector is supposed to hold onto your 'no longer referenced' objects and delete them at times when it won't slow down the application. It doesn't always work out that way though.

Share this post


Link to post
Share on other sites
The other important thing is that you have single instances of objects bouncing around in multiple classes and handlers.

So if an object shouldn't be rendered, and it disappears from the quadtree, should it be deleted? Not if you just took it out to manipulate it with the intention of putting it back in later.

Share this post


Link to post
Share on other sites
So would all this involve writing your own smart-pointers that do reference counting?

I compltely understand how smart pointers work but one thing im not sure about is how you would create a collection of them, especially since each ones type may be different.

Would you derive a smart-pointer from a non-templated base class?

ace

Share this post


Link to post
Share on other sites
Quote:
Original post by ace_lovegrove
So would all this involve writing your own smart-pointers that do reference counting?

I compltely understand how smart pointers work but one thing im not sure about is how you would create a collection of them, especially since each ones type may be different.

Would you derive a smart-pointer from a non-templated base class?

ace


Basically. You could create a base 'object' class that keeps track of references. When it's references drop down to 0, it sends itself to the garbage collector.

edt: all your classes that are 'garbage collectable' need to inherit from this base class.

Share this post


Link to post
Share on other sites
You never need a garbage collector, but what it does is alleviate the programer of the responsibility of explicitly deallocating memory. Failure to free acquired memory is a major source of problems in C and C++ programs, thus it seems desirable to have some automated way of dealing with this.

I haven't read the Enginuity articles in a while, but I believe superpig uses essentially a reference counted garbage collector (which is analagous to most "smart pointer" impelementations, when the ref count goes to 0, the object is freed). The "major" disadvantage to this approach is that each "garbage collected" object has to store two pointers; one to the actual object, and one to a shared reference count (this also means that the reference count needs to be allocated on the heap, unless you use some sort of intrusive reference counting mechanism). The other disadvantage is that it can be slower, as you have to increment the ref count on copy and then decrement when the object goes out of scope (and check to see if the ref count is zero). Most garbage collected languages (such as Java) don't use this method. Because those languages have full knowledge of the runtime enviroment, they can use more sophisticated algorithms such as mark-and-sweep, etc (basically, mark all objects that can be reached via some pointer, and then delete all the ones that aren't marked).

I however am of the opinion that the overhead incured via reference counted smart pointers is not of that great a significance, especially if properly used (try to keep the number of times the SP actually has to be copied to a minumum). Also, if I have to choose between destructors and deterministic destruction, or garbage collection with no destructors, I'll take the destructor approach any day of the week, as I think it's a more general approach that has a lot more applications (basically all the RAII stuff)

Share this post


Link to post
Share on other sites
Yes, the brunt of the garbage collecting is done through a smart-pointer/handle interface.

However, more advanced memory management schemes take advantage of handle factories, which assign some sort of constant ID to objects.

This simplifies saving and loading from disk, and various other little things here and there.

It also solves your problem of having smart pointers in a collection. As far as I know, handle-based pointers are the only way to remedy it. The reason is that smart pointers are sort of "dummy" pointers, in that they aren't actually pointers, they simply mimick them. So things like polymorphism and casting become impossible.

Share this post


Link to post
Share on other sites
Quote:
Original post by nilkn
The reason is that smart pointers are sort of "dummy" pointers, in that they aren't actually pointers, they simply mimick them. So things like polymorphism and casting become impossible.

This isn't true. Some smart pointers certainly have these problems, but not others. Take a look at boost::shared_ptr. It supports everything you've mentioned.

Share this post


Link to post
Share on other sites
Quote:
Original post by CoffeeMug
Quote:
Original post by nilkn
The reason is that smart pointers are sort of "dummy" pointers, in that they aren't actually pointers, they simply mimick them. So things like polymorphism and casting become impossible.

This isn't true. Some smart pointers certainly have these problems, but not others. Take a look at boost::shared_ptr. It supports everything you've mentioned.


Well, I stand corrected. I guess you learn something everyday!

Share this post


Link to post
Share on other sites
Surely everyone can imagine a huge project which multiple people work on, where even smart pointers aren't going to be enough to ensure there are no memory leaks.
Garbage collection is not for everything though. It's just another tool for the job. No one tool is best for every job. As always, it depends on the project, but there's usually little point in using GC on a small project.
Many of us have learned how to take care of memory management efficiently and carefully enough to not create leaks. That isn't a skill that you should merely throw away.
Don't throw away your hammer just because you now have a nail gun. However in many cases a nailgun is going to save time, particularly if you're a beginner.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement