swapoff: /dev/sda: cannot allocate memory

Sometimes you may notice a server problem with swap as below :

# swapoff: /dev/sda: Cannot allocate memory

Normally this error occurs due to insufficient memory in your server. If the amount of free memory is less than the amount occupied by swap, then you cannot turn off swap.

To get the memory details run below given commands.

# free -m or cat /proc/meminfo

“free -m” will give you the amount of memory swapped, now dropping cache will help you to free up some memory. You can use one of the below command to drop caches.

To free pagecache:
sync; echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:
sync; echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:
sync; echo 3 > /proc/sys/vm/drop_caches

The ‘top’ command will help you to find out the processes which is consuming most of memory in real time. After running “top” command you need to press “shift+m” to sort all processes memory wise.

Shift +m – Memory wise sorting
Shift +p – Process wise sorting

If the above commands will not help to free up the memory then you need to think about killing the processes which is consuming most of memory and try to turn off and on swap.

# swapoff -a

If command is run successful, then you can see the swap getting reduced. Please note this will take a few minutes to complete the operation. After that we can turn on swap with help of below command.

# swapon -a

You can use the following command to enable or disable a particular swap area.

swapoff <file-name> / <device name>
swapon <file-name> / <device name>

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts