How to return GC-d memory back to OS

Sean Kelly sean at f4.ca
Fri Aug 4 09:21:04 PDT 2006


Oskar Linde wrote:
> Serg Kovrov wrote:
>> Is it possible to make GC to free memory back to OS?
>>
>> I'm asking because I'm writing a news feed notifier/reader, and my 
>> goal for the application is to be as much memory efficient as 
>> possible, as I intent to run it on my desktop 24/7.
>>
>> In other words, I want my application to free any resources back to OS 
>> as soon as they no longer used. eg., it fetch a big document, process 
>> it store for later usage on disk cache, set a flag that new document 
>> available - then free all resources used for that task back to OS. 
>> Whatever peak memory usage will be, I want my application in idle 
>> state to have as small memory footprint as possible.
>>
>> The 'high watermark' (if thats the name) approach in GC is not way to 
>> go in my case =(
>>
>> There is not much info on GC on digitalmars site, so maybe someone 
>> know a workaround for my situation?
> 
> For a long time, even linux malloc()/free() would not return memory to 
> the OS. Usually, memory is allocated from the OS using brk() or similar 
> system calls and an application can only return memory from the end of 
> the virtual address range the application has been allocated. In 
> general, this requires a relocating/compacting GC, as even a single 
> allocated byte at the top end of your virtual address space will prevent 
> the OS from shrinking it.
> 
> But apart from needing much swap space, there are no draw backs of using 
> much virtual memory. The memory you don't use (but exists in your 
> virtual address range) will not map to physical memory and will not 
> affect your computers performance.

Just to clarify, the pages that are not in use will not map to physical 
memory.  This is another advantage of compacting collectors--they can 
empty out near-empty pages to avoid unnecessary swapping.


Sean



More information about the Digitalmars-d-learn mailing list