Search code examples
c++linuxpagingswapfile

How to prevent paging for one program / process?


I have a program that requires much memory, like 2/3 of all the physical ram. After some runtime my operating system begins to swap the program to hdd. But I need the program to respond very fast all the time, so I need to prevent paging for that process.

How can you prevent the OS to swap one process?

Thanks for any help!


Solution

  • At the start of the program, call:

    mlockall(MCL_CURRENT | MCL_FUTURE);
    

    (If you do not have the source to the program, you'll have to debauch the process with ptrace to do this).

    Be aware that this will increase the chances of memory allocations made by the process failing.