Is it a bad idea to create a cron script that empties the swap every hour?

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.


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?


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.