This question already has an answer here:

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

marked as duplicate by Scott Pack, kasperd, Jenny D, masegaloeh, Katherine Villyard Apr 19 '15 at 0:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 10
    Forget about swap. You need more RAM, and desperately. – Michael Hampton Jul 19 '14 at 15:06

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.

  • 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. – user2284570 Jul 19 '14 at 21:43
  • 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 Jul 19 '14 at 21:45
  • 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. – stimur Jul 19 '14 at 21:50
  • 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 Jul 19 '14 at 23:18
  • Thanks, this is nice way to avoid having a file being sparse. But this does not explain why it can be created at all without any additional efforts. dd reads block of zeros and writes it to the disk, one to one. No magik necessary. If you're really concerned about large blocks of zeros converted to a "metadata only" record, use /dev/urandom as input :) – stimur Jul 19 '14 at 23:47

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.

  • 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 Jul 19 '14 at 20:59
  • You can't live-extend existing swap spaces, but you can live-add more swap spaces. – Mark Jul 19 '14 at 21:07

Forget about real OS support for dynamical resizing : You are NOT using WINDOWS®! :-) Pushing the Limits of Windows: Virtual Memory
More seriously : you can increase the size of the swap file while using it, but you would need to run mkswap again, which implies unmounting it first.

I would also say using a file instead of a partition typically multiply the access time if you use a fragmented filesystem, making your probably slow system, slower.

Due your used swap size, I would set/proc/sys/vm/swappinessto 100, it may decrease disk the activity at the expanse of an higher swap space usage.
I don't know your ratio of RAM/used swap; but given the used swap size, I'm suspecting it too be high. If this true you should really consider adding some memory modules before you start to get big stall on your server.

Not the answer you're looking for? Browse other questions tagged or ask your own question.