Manually freeing up memory

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Nov 7 09:53:46 PST 2012


On Wed, Nov 07, 2012 at 06:12:52PM +0100, bearophile wrote:
> Joseph Rushton Wakeling:
> 
> >... but despite the GC.free(), memory usage stays at peak level
> >for the rest of the runtime of the function.
> 
> GC.free() usually works. Some memory allocators don't give back the
> memory to the OS, no matter what, until the process is over, despite
> that memory is free for the process to use in other ways (this is
> what often happens in Python on Windows).
[...]

I think on Posix systems, malloc/free does not return freed memory back
to the OS, it just gets reused by the process later on.

If you want to return memory back to the OS, you could call sbrk()...
but that is highly *NOT* recommended unless you know exactly what you're
doing, and you know the innards of your C library (*and* D runtime) like
the back of your hand. But it *is* the "hardcore" way of doing it. :-)

An easier workaround might be to fork() a process that constructs
whatever data structures you need, transmits that to the main process
somehow, then exit. If I understand it correctly, the large memory
allocations will be restricted to the child process, which will get
returned to the OS once it exits. (Note that you have to use fork(), not
threads, because threads share memory in the same process so you end up
with the same problem.)


T

-- 
Question authority. Don't ask why, just do it.


More information about the Digitalmars-d-learn mailing list