
If I zero out my memory pages, does that make them page in faster? - ingve
https://blogs.msdn.microsoft.com/oldnewthing/20160907-00/?p=94265
======
kazinator
If you're sure you don't need a page, then zeroing it out won't help because
when a dirty old page is becoming a page replacement victim, the typical VM
systems will _not_ check "oh, is this all zeros?" and thus will not try to
collapse it back to a copy-on-write all-zero page, as that page might once
have started out when it had been freshly allocated. The article explains that
much.

But would this be a good idea to _implement_? Could there be a benefit?

The answer is no. And the reason is that if an application doesn't require a
page any more and wants to make the underlying frame available to other
applications or other uses, the appropriate thing to do is to _unmap the
page_.

In Unix land, in addition to munmap, we also have madvise, which is a BSD
function, known in POSIX as posix_madvise. One of the things an application
can do with these functions is tell the OS that it "won't need" a range of
memory (POSIX_MADV_DONTNEED / MADV_DONTNEED).

POSIX is not very specific about what POSIX_MADV_DONTNEED means but the Linux
man page for madvise gives this for the original madvise/MADV_DONTNEED:

    
    
                  After a successful MADV_DONTNEED operation, the semantics of
                  memory access in the specified region are changed: subsequent
                  accesses of pages in the range will succeed, but will result
                  in either repopulating the memory contents from the up-to-date
                  contents of the underlying mapped file (for shared file
                  mappings, shared anonymous mappings, and shmem-based
                  techniques such as System V shared memory segments) or zero-
                  fill-on-demand pages for anonymous private mappings.
    

So it's a kind of opportunistic thing for anon private mappings: the memory
stay available while it is not needed for other things, but it can vaporize to
all zeros at any time.

~~~
pcwalton
> But would this be a good idea to implement? Could there be a benefit?

> In Unix land, in addition to munmap, we also have madvise, which is a BSD
> function, known in POSIX as posix_madvise. One of the things an application
> can do with these functions is tell the OS that it "won't need" a range of
> memory (POSIX_MADV_DONTNEED / MADV_DONTNEED).

As mentioned in the article, Win32 has something that addresses the problem
described as well: VirtualAlloc(..., ..., MEM_RESET, ...).

