How to prevent paging for one program / process?

£可爱£侵袭症+ 提交于 2019-12-04 16:20:11

问题


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!


回答1:


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.




回答2:


Well, there's mlock for locking memory (telling the kernel it may not be swapped out), but that's meant for relatively small amounts of memory, and would require modification of the program.

The other option might be to adjust Linux's "swappiness", i.e. its tendency to swap out pages. See here for an interesting discussion. That is not possible per process, however.

I'm not aware of any per-process solution for your problem.




回答3:


Use mmap() instead of malloc, and use the "MAP_LOCKED" flag. (works on linux > 2.5.37)



来源:https://stackoverflow.com/questions/2360116/how-to-prevent-paging-for-one-program-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!