How to release memory? (D2.0.30)

"Jérôme M. Berger" jeberger at free.fr
Sun Jul 5 04:41:13 PDT 2009


downs wrote:
> BCS wrote:
>> Hello AxelS,
>>
>>> BCS Wrote:
>>>
>>>> You can't. The D runtime (and most other runtimes) don't ever reduce
>>>> the amount of memory they keep in the heap. If you where to allocate
>>>> another 25MB right after that function you would see no change in the
>>>> memory usage. The good news is that with virtual memory, all of that
>>>> has almost zero cost. What matters is how much ram you are actively
>>>> using.
>>>>
>>> I want to load and send a file via network...when I load the entire
>>> file into memory it's very stupid that I can't release that memory
>>> again...
>>>
>>> OK I'll try it with the C API but thanks for your help!
>>>
>> C's runtime (malloc/free) doesn't return memory to the OS either.
> 
> Um .. yes it does. :)
> 
	No it doesn't (not always). Depending on your C runtime and the 
size of the allocations, memory may or may not be returned to the OS.

	For example, on Unix systems, there are two system calls to get 
memory from the OS: (s)brk and mmap. AFAIK, there is no way to 
return memory allocated through (s)brk. Those calls are only able to 
allocate blocs of a given size (the OS page size) and the runtime 
manages smaller and larger sizes to balance speed, and efficiency. 
Some C runtimes will allocate small blocs through (s)brk and large 
blocs through mmap, some will allocate everything with (s)brk, some 
will use mmap when the requested size is a multiple of the OS page 
size and everything else through (s)brk. Depending on the scheme 
used, the runtime may not be able to return the memory to the OS.

	Like BCS said, the only way to make sure that the memory will be 
returned is to use mmap/munmap directly (or their equivalent on your 
platform).

		Jerome
-- 
mailto:jeberger at free.fr
http://jeberger.free.fr
Jabber: jeberger at jabber.fr

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20090705/d5b38d99/attachment.pgp>


More information about the Digitalmars-d-learn mailing list