I wrote a quick and dirty program to leak memory by calling malloc repeatedly. I noticed when I ran my program in Debug configuration (in VS 2012) my program correctly consumes gigabytes of memory and keeps going until the page file is full (the Windows Task Manger reports high Working Set size). However when I run the program in Release mode the Working Set size of my program remains tiny but the Commit size keeps on growing. There is also markedly less disk thrashing or page faulting.
The MSDN documentation states that when in Debug mode, malloc is mapped to _malloc_dbg, but the documentation also states that both allocate memory on the heap, only _malloc_dbg allocates extra memory for debugging information - there is no mention of different heap behaviour (i.e. why it doesn't show up in Private Working Set in Release mode).
Pray tell, what's going on?
When a virtual memory page is committed, no physical memory is allocated until the page is accessed.
The debug malloc fills the newly-allocated memory with a known pattern, whereas the release malloc doesn't initialize it.
The initialization results in more pages of physical RAM (and more thrashing) required in debug than in release.
If you were to actually touch every page of the allocated memory, I'd expect most of the difference between the two builds to disappear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With