29

For years I have been having the same problem with my small Ubuntu configurations: the used swap space increases with time. I get the impression that this is mainly because the allocated memory never returns to RAM even though there is enough space for it, except in the case of a user action like disabling the swap.

I made a short cron command to automate this, and I have good results:

#! /bin/sh

echo "* */1 * * * root swapoff -a && swapon -a" >> /etc/crontab

But because it's more of a trick than a real solution to this problem, I'm wondering about the potential reasons it might be a bad idea, or how could I improve this script to make it a little more clever?

6 Answers6

51

Using it like that: yes, bad. You really need to check if there is enough memory available before you turn swap off. See https://askubuntu.com/a/90399/15811 for a better version.

Also: are you sure about this? Swap being allocated does not mean swap being used. The command vmstat, columns si (swap in) and so (swap out). If those remain 0 you got another problem. In my experience swap is hardly used, and you might not be using it thinking it does not empty it but there is nothing to empty.

Rinzwind
  • 309,379
42

I'd say it's a bad idea. If you think you have free memory and an active process isn't being moved from swap to RAM, then either you don't have as much free memory as you think you do, or the process isn't as active as you think it is.

If an active process continues to be swapped, you should be fixing whatever it is that's causing pressure on the memory. If it isn't an active process, what's the big deal?

muru
  • 207,228
35

It's a bad idea.

The kernel starts copying (not moving) data to swap long before physical memory is close to being full, because if some process ever requires a lot of memory, any page that already has a valid copy in swap can be reused immediately without requiring another write to disk.

Generally that happens mostly for pages that haven't been accessed in a long time, which is a good indicator that it is unlikely that they will be accessed soon.

If you explicitly discard the copies, that brings no benefit, because the data still exists in RAM, but may cost you speed when some process wants to allocate a lot of memory and swapping becomes necessary.

The kernel will also always use swap space as soon as physical memory is above 50% full, so these numbers will be non-zero even if you have enough memory installed.

22

This is a bad idea. If this were useful, the Linux kernel would implement it this way. I do not believe there is a reason to change more than a few tuning parameters, as such a simple shell script most likely is not more clever than the algorithms of the kernel developers.

You basically have two cases:

  • The processes in swap space are not used anyway. Why do you want to pull them back into RAM?
  • There is little RAM, so they get swapped out and you pull them back into RAM. Then your system will put them into swap again as soon as possible.

So there are two main points:

  1. First, your system will be slow when there is too little RAM to run all your programs at once. Swap will help you to run more programs, but not to switch quickly to a rarely used one, which may be swapped out. No swap may get the rarely used one killed or send the currently used one an out-of-memory exception.
  2. Second, swap is a good thing and so is having stuff in swap, as you have free RAM at cost of programs you're currently not using anyway.

Despite not getting out-of-memory problems with too many programs, some programs may allocate memory based on the currently free RAM (maybe your browser will use more memcache and you can browse faster) and the kernel can use free RAM for disk caching and similiar optimizations. When you force your swap to be empty, the kernel will drop its read cache and e.g. starting a new Firefox instance will take longer than when Firefox is still in the disk cache.

If you want to tune the behaviour of the kernel, see the swappiness parameter.

Two additional ressources contributes by @peter-cordes:

If you really want to have empty swap, you can turn off swap permanently. I do not see why having it on for an hour and then emptying it has advantages over not having swap.

allo
  • 671
5

You can achieve the same results by telling the kernel to free up the caches:

echo 3 > /proc/sys/vm/drop_caches

This way you avoid the brief moment of possible memory starvation and leave the kernel decide what is necessary and what can be discarded.

Alex C
  • 300
0

Contrary to the common idea SWAP in itself is not bad.
What actually slows your system down is the kernel activity that moves Data from the RAM to the SWAP and back into the RAM, the swappiness.
The System does this automatically as it is configured with the swappiness.
This makes that Memory from inactive processes is dumped to the Harddisk Swap Partition.
I myself worked for years with a Machine that did not have so much RAM Memory and I always had some SWAP Memory used. Still my machine worked fine untill you start to move the the memory back into RAM, perhaps by trying to close an open application. Then the Work Load started to increase.

  • So by cleaning out constantly you SWAP Memory the Work Load on your machine will considerably increase.
  • Running Applications that have their Memory on the SWAP Partition can corrupt in their execution.

Rather I would suggest you study closly which application uses your memory on the command line with the htop application and decide to close some Application. The gnome-system-monitor can give you good insight as well, in its Process-Tab.
If you have big applications that use a lot of RAM. Do not run them all at once.