How to add a swap file in Linux Azure virtual machines

Applies to: Virtual Machine running Linux

On a Linux Azure virtual machine, you encounter an out-of-memory (OOM) error. For example, the System log shows the following error:
localhost kernel: Out of memory: Kill process 2154 (oom) score 844 or sacrifice child
This issue may be caused by the lack of a swap file. You can add a swap file to increase the virtual memory in Linux Azure virtual machines. To do this, follow these steps.
  1. To create a swap file in the directory that's defined by the ResourceDisk.MountPoint parameter, you can update the /etc/waagent.conf file by setting the following three parameters:
    Note The xx placeholder represents the desired number of megabytes (MB) for the swap file.
  2. Restart the WALinuxAgent service by running one of the following commands, depending on the system in question:
    Ubuntu: service walinuxagent restart
    Red Hat/Centos: service waagent restart
  3. Run one of the following commands to show the new swap apace that's being used after the restart:
    dmesg | grep swap
    swapon -s
    cat /proc/swaps
    file /mnt/resource/swapfile
    free| grep -i swap
  4. If the swap file isn't created, you can restart the virtual machine by using one of the following commands:
    shutdown -r now
    init 6
For more information, see Ubuntu Cloud Images and SWAP space in Windows Azure Virtual Machines running pre-built Linux Images.