GC and memory leaks
Kevin Bealer
kevinbealer at gmail.com
Mon Nov 12 23:35:11 PST 2007
Ald Sannes Wrote:
> Vladimir Panteleev Wrote:
>
> > On Sun, 11 Nov 2007 18:34:03 +0200, Ald Sannes <aldarri_s at yahoo.com> wrote:
> >
> > > Yet the 800 Mbytes of memory are not being freed until the program terminates.
> >
> > AFAIK, DMD's GC does not release memory back to the OS, ever.
> >
> > Also, minimize() does nothing, and genCollect() does the same thing as fullCollect().
> >
> > One thing you could try is Tango's GC, which in my experience behaves better in some circumstances. You can use Tangobos[1] to keep the Phobos API and use Tango's runtime (which includes the GC).
>
> .......
> ...
> Ok, let's then manage memory manually. Where should I look for the leaks? I already delete everything I declare; guess some memory is allocated behind the scenes?
I think even malloc() does not free memory to the OS. Getting memory from the OS and returning it to the OS are expensive operations so most implementations will allocate chunks of memory.
If you want to make sure memory is returned to the OS, you can create files and use mmap(). When you close the file and munmap the memory, the OS will truly get the memory back.
Some operations like "new ObjectName", associative arrays, dynamic arrays, and "x ~ y" will automatically use normal memory though, so you would be restricted to using "struct" instead of class, and doing certain other things the "hard way".
But to be honest, I can't think of a good reason (in general) to do these things... whether the O/S owns the free memory or the application's garbage collector does is unlikely to hurt anything, except for your attempts to get an accurate picture of what is going on.
If you want to see if memory is being leaked, run the method that might be leaking memory in a loop. If the loop iterates 10 times and you still only have 800 MB of application size then I would guess that there is no leaking. If the application keeps getting bigger with each iteration, then that probably indicates a problem.
Kevin
> > > To parse HTML, I used std.regesp.replace(). On some files, it loops, ate all memory in less than a minute and crashed.
> >
> > std.regexp has some known issues. Unless you're in the mood to debug and fix it (which would be making all of us a favour), for real work you might be better off finding some libpcre wrappers. Just in case, first check that your input is valid UTF-8 - that got me once (broken UTF-8 sequences make std.regexp crash and burn).
> >
>
> Thanks. Actually, since all I need is to find text in HTML, a FSA, built with a huge two-level switch structure, proved to be sufficient.
>
More information about the Digitalmars-d
mailing list