Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » My memory problems are over

This thread is locked; no one can reply to it. rss feed Print
 1   2 
My memory problems are over
axilmar
Member #1,204
April 2001

I've decided that I had enough of memory problems with c++...so no more manual memory management for me. I will use Boehm's gc from now on, which is extremely good and very very fast.

Why am I saying this? well, I've just saw another memory problem topic in the forum, and I've thought I'd share my two bits of wisdom.

People, use garbage collection! even in games! at this day and age, computers are fast enough to handle it, and the collector is very mature. Thousands of objects can be collected within a few milliseconds.

GullRaDriel
Member #3,861
September 2003
avatar

glib ftw

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Goalie Ca
Member #2,579
July 2002
avatar

I'll stick to using RAII and wrapper classes such as smart pointers. The only hole left from that are cyclical dependencies (which can technically be addressed by a little thinking and good use of weak pointers). Most of the time either people share pointers or someone owns a pointer.

All of these are examples of objects which own memory and cleanup automatically when they get destructed. They die when they go out of scope.

-------------
Bah weep granah weep nini bong!

axilmar
Member #1,204
April 2001

Quote:

I'll stick to using RAII

Why not use gc? gc has numerous advantages over reference counting.

kazzmir
Member #1,786
December 2001
avatar

Part of my research at school is looking at precise garbage collectors with C code. I have a tool that converts normal C code to use a precise garbage collector, if you want to try it.

Jeff Bernard
Member #6,698
December 2005
avatar

I don't need garbage collection... it's not that hard to delete a pointer or destroy a bitmap...

--
I thought I was wrong once, but I was mistaken.

SiegeLord
Member #7,827
October 2006
avatar

Lack of garbage collection is still faster than the presence of garbage collection. All modern OS's (AFAIK) free the application's memory after the application closes. Things like games run for a couple of hours at a time on average, so it does not matter if they leak. I want my programs to be fast, so I purposefully leak many of my objects. I take care not too create too many, and it all works out in the end.

If you are creating objects every logic step you have a problem with the design, the solution is to alter your design, not to add a blind garbage collector I.e. there is no need for a garbage collector if there is no garbage.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

kazzmir
Member #1,786
December 2001
avatar

Quote:

I want my programs to be fast, so I purposefully leak many of my objects.

Remind me never to play any of your games.

SiegeLord
Member #7,827
October 2006
avatar

If an object is created only once there is no difference whether it is freed or not. If a program leaks and it's leaks do not get worse over time there is absolutely no difference for the end user, since in the end the program uses the same amount of memory, but because of a lack of an explicit gc it runs faster.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Karel Kohout
Member #5,968
June 2005
avatar

I'm depressed. I expected a revolutionary idea improving your memory (and helping me study for exams at 9 a.m., twelve hours from now including sleeping and 1h in the underground) and all you suggest is using substandard obscure compiler :-/

kazzmir
Member #1,786
December 2001
avatar

Yes, certainly for singleton objects you don't have to free them. I probably dont reclaim those objects myself. But you made it sound like you leak various small objects because its easier to not deal delete them manually. Over time this might cause problems.

As a reasonable compromise you could just run the gc when the current level ends or some other normal pause in the game occurs. This is pretty much how I deal with all the objects in my latest game. Allocate things during the game and deallocate everything at the end of each level.

Goalie Ca
Member #2,579
July 2002
avatar

Quote:

Why not use gc? gc has numerous advantages over reference counting.

Like what? Memory pooling? I can do that with precision using one of the many simple libraries. The only advantage i see is that it deals with cycles.

One of the problems with GC's is that you go from manually releasing memory to manually releasing resources. In a gc environment you never know when an object is going to be disposed of. This kills all hope of using the destructor to close the file handle etc.

edit: doing even get me going on about real-time collectors. What a load of crap.

edit2: SiegeLord.. WTF!

-------------
Bah weep granah weep nini bong!

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

SiegeLord.. WTF!

I'll second that. Memory leaks have cause a few of my programs to crash before I caught them, running fine on my system but not on older ones. Proper allocation and deallocation are staples of proper programming; to say "fsck it" and just go is madness.

Quote:

If a program leaks and it's leaks do not get worse over time there is absolutely no difference for the end user

You need to recognize how colossal this "if" is.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Vanneto
Member #8,643
May 2007

Its not hard to create/delete object in small/medium sized projects. But in projects like games or browsers ( Firefox ) it can be a problem.

In capitalist America bank robs you.

kazzmir
Member #1,786
December 2001
avatar

Quote:

One of the problems with GC's is that you go from manually releasing memory to manually releasing resources. In a gc environment you never know when an object is going to be disposed of. This kills all hope of using the destructor to close the file handle etc.

Its called a finalizer. http://www.hpl.hp.com/personal/Hans_Boehm/gc/finalization.html

axilmar
Member #1,204
April 2001

Quote:

Like what? Memory pooling? I can do that with precision using one of the many simple libraries. The only advantage i see is that it deals with cycles.

Not only it deals with cycles, but:

1. it's way faster than reference counting.
2. more readable code.
3. faster and easier to write for.
4. works with C.
5. works in a multithreaded environment.

Quote:

One of the problems with GC's is that you go from manually releasing memory to manually releasing resources. In a gc environment you never know when an object is going to be disposed of. This kills all hope of using the destructor to close the file handle etc.

You don't have that problem in C++. You can continue declaring your instances on the stack, i.e. use RAII as you see fit.

In fact, one of the reasons I'm sticking to C++ is RAII (and templates, of course).

Quote:

doing even get me going on about real-time collectors. What a load of crap.

IBM disagrees with you!!!

Goalie Ca
Member #2,579
July 2002
avatar

Yeh I know about finalizers. They still aren't perfect. Nothing ever is though. I first learned all about them when dealing with dispose and finalize in c#.

GC's are not "way faster" than reference counting. Maybe gc's built upon reference counting are slower though.
Readable code.. if you consider boehm extensions readable and pointers not.
Faster and easier to write for.. yes and no
Yes gc's do work with c. Smart pointers require c++ to be syntactically clean. Otherwise there are a lot of winapi style dealings with structs and manual counting. (i think that's how winapi deals with it. i might be confusing them with another)
multithreaded.. Boost's pointers are thread safe. What i haven't seen is a concurrent GC.

-------------
Bah weep granah weep nini bong!

axilmar
Member #1,204
April 2001

Quote:

GC's are not "way faster" than reference counting.

They are. Assigning a reference counted pointer results in dozens of instructions.

Quote:

Readable code.. if you consider boehm extensions readable and pointers not.

Boehm's gc does not have any extensions in the language. Instead of using malloc, you use GC_malloc.

Quote:

multithreaded.. Boost's pointers are thread safe.

And slow, because they have to InterlockedIncrement/Decrement ref counts.

Quote:

What i haven't seen is a concurrent GC.

Boehm's GC is concurrent: it will start N threads for marking and collection, where N is the number of available CPUs, if compiled with PARALLEL_MARK.

SiegeLord
Member #7,827
October 2006
avatar

Quote:

You need to recognize how colossal this "if" is.

If your design is good it is not colossal at all. If a program has many leaks it means that you are using heap allocations often. Heap is not meant to be used in that way, you are supposed to allocate and deallocate it very very sparingly. A good design does not need gc. Specifically, imo, garbage collection encourages lazy coders.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

kazzmir
Member #1,786
December 2001
avatar

Quote:

Heap is not meant to be used in that way, you are supposed to allocate and deallocate it very very sparingly.

??? Where do you get this stuff?

Vanneto
Member #8,643
May 2007

Game programming All in One!

In capitalist America bank robs you.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I've implemented a simple memory manager class with vectors of different common pointer types that lets me deallocate them all when the object goes out of scope. Not so useful for cross function purposes , but handy enough to use when there are heavy or extensive memory allocations in a single frame. Still have to write one line for allocation and another to add it to the classes vector , but I can just write the lines successively and be done with it.

Goalie Ca
Member #2,579
July 2002
avatar

Once i get this paperwork done i will sit down and study the papers on the boehm gc. Admittedly i haven't looked at these things in quite a while. Mostly if i want "memory woes gone" then i use a language that has a gc and has other nice things as well. Auto_ptr's have ownership and they are fast. Only penalty is in assignment. Then shared_pointers only have penalty in assignment as well.

And sieglord.. ya if you are frequently allocating and deallocating then maybe you want to set up a memory pool. Memory pools have very very fast allocations.

-------------
Bah weep granah weep nini bong!

HoHo
Member #4,534
April 2004
avatar

I wonder if I'm missing something but why do people use that much manual memory management? I have written a C++ client/server application that have around 10kloc combined and about ten or so new blah in it with each of those called once per program lifetime (some classes cannot live in stack). There are a lot of stuff constantly changing and I've yet to see a leak even with weeks of uptime. I simply use STL* a lot and it works great!

*) Those programs do not use QT, with QT things would be even simpler even though there would be a lot more manual memory allocations there. QT FTW ;)

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Carrus85
Member #2,633
August 2002
avatar

Garbage collection is great, in theory, as long as you keep in mind some drawbacks:

  • Encourages lazier programming in general (good for quick hack-up programs, bad when the runtime performance of an algorithm is very, very important. Also encourages "static abuse" in a lot of GC-able languages (for example, concatenating two strings together in Java creates a whole new string, because strings are immutable; major performance bottleneck for those who don't watch their step).

  • Adds a degree of non-determinism in program runtime (e.x. Writing a ChessAI in C# can get kinda dicey when you have a hard turn time limit.). (Referencing counting also has this problem, but not as bad. Incremental Garbage Collectors also tend to avoid this problem.)

  • Sometimes you just want control of the memory allocations.

  • Doesn't work with RAII (if you must allocate everything on the heap, ala java). And no, finalizers are not a substitute for RAII due to their non-deterministic execution; you know the finalizer will run, but you don't know when it will run, which can be a huge problem if you expect it to run immediately after going out of scope. (Thus, you end up having to finally blocks and other garbage to clean up after yourself, which isn't as clean as RAII.)

Basically: If the program has some hard deadlines, requires as much efficiency as possible (say, your kernel or some math function that is called thousands of times), or is highly interactive, then GC's should be used with extreme caution or avoided entirely. GC's work pretty much everywhere else, though.

Now, that given, the description of Boehm's gc via GC_malloc, it should be possible to just globally override new and delete and use them (assuming GC_malloc doesn't take any other strange arguments or have screwy side effects, as well as a GC_free existing somewhere). This would give you a really interesting benefit, in C++ in particular; you could use RAII where you want, use regular old new for everything else, and get garbage collection for free (in theory). I'd have to look up specifics and run some tests, though. :)

 1   2 


Go to: