Conversation

Replying to
MEM_RELEASE will free the mapping completely like munmap on Linux, making the address space usable for other things. MEM_DECOMMIT will release the memory but hold onto the reserve address space like clobbering a mapping with mmap + MAP_FIXED + PROT_NONE on Linux.
2
2
Replying to
What does "hold onto" mean in this context? Does it mean the process continues to reserve the memory but the content pages are discarded?
1
1
Replying to
It has the memory mapping but you can't use the memory because it's not committed, i.e. it can't assign pages to it if you touch it. It's just like having PROT_NONE mappings on Linux. The reason this exists is because Windows is designed around memory accounting, not overcommit.
1
1
Replying to and
Linux has a memory accounting mode via /proc/sys/vm/overcommit_memory and it just treats fresh PROT_NONE mappings the way Windows does MEM_RESERVE mappings. Not really a way to do MEM_DECOMMIT on Linux other than clobbering with a new PROT_NONE mapping via mmap with MAP_FIXED.
2
2
Replying to
The codebase I'm editing has a linux implementation but not a windows implementation... the linux implementation is madvise(p, size, MADV_DONTNEED) I'm replacing it with MEM_DECOMMIT
1
1
Replying to
The advantage of MEM_DECOMMIT and MEM_COMMIT is that the OS knows you don't need it during that time. MADV_DONTNEED still keeps it as memory you can start using it at any point, and in memory accounting mode it gets counted against you.
1
3
Replying to and
So, code on Linux that cares about supporting memory accounting mode well needs to switch to using mmap with MAP_FIXED to clobber a region back to a fresh PROT_NONE mapping. Projects like jemalloc decide between MADV_DONTNEED and that based on the overcommit mode in /proc.
1
2
Replying to and
Windows also has MEM_RESET which is similar to MADV_FREE. On either Windows or Linux, those mark the pages as being lazily freed under memory pressure. On Linux, the kernel detects when you write to it and stops doing it. Windows requires MEM_RESET_UNDO to explicitly undo it.
1
1
Replying to and
So, for example, you could generate something that takes a lot of memory but won't be needed again for a while and MEM_RESET it. Once you need it again, MEM_RESET_UNDO will try to get it back. If OS ran out of memory, it would have freed it, and it will tell you that it's gone.
2