I'm trying to build a cache package for golang that relies on mmap to store large amounts of content in memory, even more than the system has. The cache doesn't need to be persistant; every time the app run, it loads the cache from redis.

But, what happens when the program crosses the max limit of physical memory? The next allocations happen from disk? If the kernel reclaims the memory from mamp, what happens with the content? It goes away? Do I need to fsync the mmap to disk to prevent this kinds of thing? Or does the kernel automatically do this?

1 Answer 1


When you run out of physical memory, the system will start evicting some least recently accessed memory pages to the swap partition. Normally, it's entirely up to the kernel to select which pages these are. They might not be your pages but rather belong to another process that hasn't been used in a while.

If you want to meddle, you can use the madvise(2) syscall to advise your kernel on your memory usage strategies so that it can adapt accordingly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .