5

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!

Jakub
  • 151
  • 1
  • 3

5 Answers5

7

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.

  • +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. –  Jul 27 '11 at 07:38
2

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).

  • 4
    Swappiness [doesn't do quite what you might expect](http://lwn.net/Articles/100978/) -- experiment by all means but don't guess a value and assume it will work. –  Jul 27 '11 at 07:36
1

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.

Steve Dispensa
  • 261
  • 1
  • 4
1

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.

DDJ
  • 11
  • 1
0

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.

David Schwartz
  • 31,215
  • 2
  • 53
  • 82