Garbage Collection in C++?

Started by
4 comments, last by Lenox 18 years, 7 months ago
I'd like to see a lot of people's views on Garbage Collection, :P. I googled the site and found this: evolution's topic from March of 2005 where Fruny posted a link to a GC and other people provided nice information, but I'd like to see what everyone thinks about it 3 years later. I'm also trying to soak in some information about C++ GCs like a sponge, so if anyone has any further information, feel free to post. :P [Edited by - Lenox on August 28, 2005 12:19:56 AM]
Advertisement
you know, people talk so much about garbage collection but I really don't see the need. Maybe it is just my programming style, or the types of games that I like to work on, but I just don't end up doing a significant amount of memory management. I used the std types a lot, so someone already wrote the memory management there. I use a lot of local variables that go out of scope, rather than allocating willy-nilly. Most of the stuff that I use "new" on I keep around until the end of the program and so I don't even bother deleting them because they will get freed when the program terminates.

So, at least for me, a GC system would be mostly cosmetic I think. Plus languages that usually have GC tend to have limited destructor support. Destructors are cool! They are great for things like mutexes.
I'm interested in making a GC not for the end of the program, no, but for everywhere in between. I'd track memory usage and if it gets too high, then I find some stuff that is able to be deleted and then delete it. Also, does Linux clean up after a program as well?
Quote:Original post by Lenox
Also, does Linux clean up after a program as well?


Any modern operating system will clean up some resources (memory, file handles). Most can't clean all of them up, however (temporary files not explicitly marked so/placed in a temp folder, named handles, GDI resources, etc). I've heard rumors that certain uber-ancient (historical) OSes did not clean up basic resources such as memory after a program. Also, certian constrained platforms will not release resources manually - consoles, some older PDA architectures most likely, etc.

The main purpouses for freeing all your memory in a non collected program are:
1) Ease targetability for targeting such platforms.
2) "Correctness" - most memory debuggers rely upon the programmer freeing all memory (or trying to) at the end of the program to report memory leaks (everything that wasn't freed). Of course, GC dosn't suffer this problem.
3) Stability. If a program continues to allocate resources, unless they are freed as well, the program will not be able to run indefinately (required for some applications - e.g. web hosting).
4) Lower profile. Resources freed are immediately available for reuse. In situations where real time preformance isn't needed, garbage collection can improve overall preformance by only releasing resources when they're actually needed - which might be never.
Quote:Original post by Lenox
I'm interested in making a GC not for the end of the program, no, but for everywhere in between. I'd track memory usage and if it gets too high, then I find some stuff that is able to be deleted and then delete it. Also, does Linux clean up after a program as well?


If you want GC, why not program in Java? The whole language has GC built in from what I understand. Note that this GC is reputed to make Java slower than C++. If you build your own GC, you should be aware of how it will affect the performance of your application, especially if you already have high CPU load and are making a real time graphics based program.
Well, I think I may just use smart pointers. ( Ex. Loki::SmartPtr ) I'm going to see how these work out.

This topic is closed to new replies.

Advertisement