We had a problem on one of the servers which I help administer.  Every now and again RAM usage would max out and SWAP would then be used – this would sometimes max out and bye bye server, normally this happened for only a few seconds and all was fine – however if it lasted more then when SWAP ran out – the server ground to a halt and needed a manual restart.

The server in question is quite busy – but only had 512Meg of RAM (this has since been rectified) historically as it wasn’t that busy at first several years ago has slowly grown as websites on it have grown in popularity and databases have grown.

So an immediate fix was required – it had a swap partition – couldn’t create a new one, it would mean downtime as we are using the hard-drives!

So added a swap file – here’s how to do it:

  1. Change to a directory (on a partition) where you have lots of space – e.g. /home  “cd /home”
  2. Create a file swapfile using dev null – the following command will create a 4meg file:  “dd if=/dev/zero of=swapfile bs=1024 count=4194304”
    This creates an empty file called “swapfile” with a block size of 1024bytes and creates 419304 (to get the size do 4194304 / 1024 then divide by 1024 to get the size in gigabytes).
  3. chmod it so it can be used “chmod 600 swapfile”
  4. format the file to a swapfile type so the system can use it “mkswap swapfile”
  5. In order to use it – turn it on “swapon swapfile”

At the end of this if yo do the following “cat /proc/swaps” to see your swapspace.  It’s now larger by a lot and hey presto.  Of course a long-term fix has to be more ram if possible or tweak your setups! (or a new server!)

This doesn’t get setup to activate on boot – mainly because I didn’t need this – a temporary fix and therefore didn’t want todo this as the RAM would be upgraded soon, but if you need more memory – and don’t mind it being slow for stability then this is a possible solution.