Search code examples
memoryswapmmap

Is it possible to protect certain data from a swap-out operation?


I have an issue.

I want my data is always kept in the main memory. So when I allocate the memory by malloc or mmap, is it possible to protect this memory to be selected victim of a swap-out operation?

Can you give me some advice on these issues? Thank you


Solution

  • Yes, use the mlock(2) system call.

    However, read the man page carefully, including the NOTES section; there are a number of restrictions and caveats. In particular, there may be limits on the amount of memory that an unprivileged process may lock.