View previous topic :: View next topic |
Author |
Message |
bane
Joined: 01 May 2007 Posts: 41 Location: Pancevo, Serbia
|
Posted: Thu Jul 19, 2007 9:08 am Post subject: Garbage collecting and memory leaks |
|
|
Ok, I spent last 12 h busting my brains with this nutcracker and I cant help feeling I don;t know anything anymore
As I get it: D is garbage collected. As far as variable goes out of scope (local variable in a function, for example), it should be collected. Someday. But it will. How come that following code STILL eats 50MB of RAM when it is done?
Code: |
import std.gc;
import std.stdio;
void foo(){
char[] x = new char[10000000]; // about 10MB of RAM
//delete x;
}
int main() {
for (int i = 0; i < 5; i++)
foo();
writefln("done");
while(true){} // ugly way to pause program
return 0;
}
|
And when I uncomment that line that deletes x, instead of freeing all memory, there is still 10 MB of RAM used. It's like this both on windows and linux. It doent helps with manual freeing memory and deleting, there is allways a big chunk left that goes on my nerves big time.
I can't make 24h running high load server like this. I don't have 100000Terabytes of RAM for all this leakage Anybody there knows a good D plumber or is it just me ignoramus? _________________ "I apologize only for my spelling" - a quote shamelessly stolen from some guy at unknown forum. |
|
Back to top |
|
|
pragma
Joined: 28 May 2004 Posts: 607 Location: Washington, DC
|
Posted: Thu Jul 19, 2007 9:28 am Post subject: |
|
|
Here's the deal: D's garbage collector is greedy. It is only capable of expanding the heap, but does nothing to return heap memory back to the OS. This strategy is a huge win if you're working with lots of discrete objects like strings and objects, but (as you've found out) gets in the way with large allocations like this.
If you're allocating large swaths of memory in this fashion, you might be better off with not controlling them via the GC at all - odds are, large block allocations like this are raw data, so they shouldn't even be scanned by the GC in the first place.
So in a nutshell: use malloc/free (std.c.stdlib) wherever the GC behavior gets in the way. _________________ -- !Eric.t.Anderton at gmail |
|
Back to top |
|
|
bane
Joined: 01 May 2007 Posts: 41 Location: Pancevo, Serbia
|
Posted: Thu Jul 19, 2007 10:12 am Post subject: |
|
|
So, it is The Curled Braces Language Curse all over again: you can't do squat unless you mess with the malloc/free yourself. Good I like those two _________________ "I apologize only for my spelling" - a quote shamelessly stolen from some guy at unknown forum. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|