How to save RAM in D programs (on zero initialized buffers)

Marco Leise Marco.Leise at
Thu Feb 9 04:00:56 PST 2012

Am 09.02.2012, 11:55 Uhr, schrieb Kagamin <spam at here.lot>:

> I guess, calloc will reuse blocks too, so if you run the compressing  
> function twice, it will reuse the memory block used and freed previously  
> and zero it out honestly.

You don't understand how it works. calloc gives you exactly 0 KB of  
memory. There is nothing to zero out :)
There is on page, a block of 4096 bytes somewhere in the kernel, that is  
all zeroes and read-only. If you allocate memory you get a bunch of  
references to it. Or in other words, a zillion views on the same 4096  
bytes repeating over and over. Only once you need to write to it, will  
happen what you say. The zero page is 'copied' into some (probably  
previously freed) page. This is equivalent to zeroing out the target page.
The main difference here is that the zeroing out happens with a 'lazy'  
keyword attached to it.

More information about the Digitalmars-d mailing list