Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We have a critical application which uses pretty much of our RAM memory. At most time the application stays in RAM, however there are some situation (e.g. when some other applications and deamons start using more RAM) which make it going to SWAP meaning that our application starts to respond very slowly.

Are there any ways we could prevent our application from going to SWAP?

Thanks guys!

share|improve this question

migrated from Jul 29 '11 at 1:52

This question came from our site for professional and enthusiast programmers.

Not likely, if your RAM is full there's no where else for it to go. – Paulpro Jul 27 '11 at 7:21
Is turning off swap an option? – Chris Morgan Jul 27 '11 at 7:21
up vote 5 down vote accepted

Turn off swap, or use mlockall() to prevent process memory from being swapped out. There are process and system limits though of how many pages can be locked, which may need to be adjusted for your purposes.

share|improve this answer
+1 for mlock but turning off swap will make programs fail whereas before they continued to run slowly but correctly. I only ever consider disabling it for security reasons. – spraff Jul 27 '11 at 7:38

Have a look here. By echoing a value between 0 and 100 to /proc/sys/vm/swappiness, you can control the eagerness of swapping (0 being not very eager, while 100 is very eager).

share|improve this answer
Swappiness doesn't do quite what you might expect -- experiment by all means but don't guess a value and assume it will work. – spraff Jul 27 '11 at 7:36

You need to fix the underlying reason you're going to swap in the first place. That means tuning your app or adding RAM. Swapping is the symptom, not the problem - incorrect hardware sizing for your app (or a badly tuned/architected app) is the problem.

share|improve this answer

Swapping not Allways is a app or need of RAM fault, if you are at the default of most distros, and your application have a lot of inactive pages, when your system start to load files the cache will grow and keep "files" in memory as cache to try to avoid more IO, but this not allways mean the system is being smart for your needs, like a backup of a file system that happens at night and grow a very large cache of needless data swapping your crucial application to disk.

share|improve this answer

Preventing your application from swapping will likely make it respond more slowly. The operating system is only swapping out your application because it has no choice. If you prevent it from doing so, the total I/O load on the system will increase as the other processes have to swap in and out more because you're hogging all the physical memory. This will slow down your I/O too, and the result will be a disaster.

If a machine doesn't have enough physical memory for its workload, then it will go slow. More physical memory or less workload is the solution. Brute tuning like you're suggesting will tend to make everything worse.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.