6

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
Peter Mortensen
  • 2,319
  • 5
  • 23
  • 24
Nyxynyx
  • 1,449
  • 10
  • 37
  • 47

3 Answers3

9

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.

David Houde
  • 3,160
  • 1
  • 15
  • 19
stimur
  • 894
  • 5
  • 11
  • No, this may create a [file with some blocks as sparse](https://en.wikipedia.org/wiki/Sparse_file). Then, the swapon command can refuse to swap on it telling :`swapon: swapfile has holes`. – user2284570 Jul 19 '14 at 21:43
  • http://www.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
  • 1
    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 `mkswap`on it :`swapon`will 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
  • 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)*. – user2284570 Jul 20 '14 at 00:19
1

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.

Craig Watson
  • 9,370
  • 3
  • 30
  • 46
  • 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](http://en.wikipedia.org/wiki/Flash_memory) 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
0

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.

user2284570
  • 178
  • 12