How to increase swap space on a live server

19,684

Solution 1

I agree with comment about more RAM. Answering your question: you can create either additional swap partition (if you have extra space on a hard drive) or create swap file.

dd if=/dev/zero of=/path/to/swap_file bs=1024 count=${size_of_additional_swap}
mkswap /path/to/swap_file
swapon /path/to/swap_file

The more swap in use the more load on your hard disk and definitely the slower the system.

Solution 2

As Michael Hampton alluded to in his comment, you need more RAM, not swap.

Swap space is generally only used if your applications cannot fit into the available RAM - this is typically a different use pattern than Windows, where the page file is typically used for infrequently-accessed pages.

If your swap space is not residing on flash storage, then it will likely perform a lot worse than actual RAM.

However to answer the question, you cannot live-extend swap. You will need to unmount/swapoff, extend the underlying storage, then remount/swapon.

Share:
19,684
Nyxynyx
Author by

Nyxynyx

Hello :) These days its web development: PHP, Codeigniter, node.js, Javascript, jQuery, MySQL, mongoDB, HTML/CSS

Updated on September 18, 2022

Comments

  • Nyxynyx
    Nyxynyx over 1 year

    On Ubuntu 14.04, the swap file has to be increased as the memory and swap usage are reaching 100% usage.

    On a live production server, can the swap be increased with minimal disruption to the service?

    Output of free -h:

                 total       used       free     shared    buffers     cached
    Mem:          3.9G       3.4G       435M        24K       1.2M       7.5M
    -/+ buffers/cache:       3.4G       443M
    Swap:         8.0G       6.6G       1.4G
    
    • Michael Hampton
      Michael Hampton almost 10 years
      Forget about swap. You need more RAM, and desperately.
  • user2284570
    user2284570 almost 10 years
    No, flash have a limited write cycles per flash cell : normally EEPROM can only be erased entirely. but flash memory are divided in cells (aka physical drive sector) as a way to overcome this limit. But doing this limit the number of erase cycles to somethings like 15,000,000 for the best hardware quality. This is typically reached in 1 mouth when using intensive SWAPPING. The right solution (which is used in fault tolerant virtualisation) is to use SAS hard disk, typically providing 30K RPMs.
  • user2284570
    user2284570 almost 10 years
    No, this may create a file with some blocks as sparse. Then, the swapon command can refuse to swap on it telling :swapon: swapfile has holes.
  • stimur
    stimur almost 10 years
    faqs.org/docs/linux_admin/x1762.html " One good way to create the swap file without holes is through the following command: $ dd if=/dev/zero of=/extra-swap bs=1024 count=1024
  • stimur
    stimur almost 10 years
    from man page of swapon: swapon may not work correctly when using a swap file with some versions of btrfs. This is due to the swap file implementation in the kernel expecting to be able to write to the file directly, without the assistance of the file system. Since btrfs is a copy-on-write file system, the file location may not be static and corruption can result. Btrfs actively disallows the use of files on its file systems by refusing to map the file. This can be seen in the system log as "swapon: swapfile has holes." One possible workaround is to map the file to a loopback device.
  • user2284570
    user2284570 almost 10 years
    No, this as nothing to do with btrfs as I've seen this behaviour on Reiser4; HFS+ and ext4. the first line would be :dd if=/dev/zero bs=1024 count=${size_of_additional_swap} | cp --sparse=never /proc/self/fd/0 /path/to/swap_file. I recognize this is very rare with that kind of command... but I experienced it several times. Just create an empty sparse file with zeros inside it, then run mkswapon it :swaponwill fail.
  • user2284570
    user2284570 almost 10 years
    Yes, but I assure your way can create a sparse file. The kernel expect to be able to write to the file directly, without the assistance of the file system. This can't be done is the file is sparse (so this the same kind of error with network filesystems).