System performance is mostly affected by memory and disk IO so it should be configured to fit your usecase for best results.
Wikipedia
In computer operating systems, swapping or paging is a memory management scheme by which a computer stores and retrieves data from secondary storage for use in main memory. In this scheme, the operating system retrieves data from secondary storage in same-size blocks called pages. Paging is an important part of virtual memory implementations in modern operating systems, using secondary storage to let programs exceed the size of available physical memory. More...
The Basics
The most important SWAP option is the "swappiness" which defines a procentual value how it will be handled.
0 | Swap is disabled. Until Kernel 3.4, the kernel would swap only to avoid an out of memory condition. |
1 | Kernel version 3.5 and over: Minimum amount of swapping without disabling it entirely. |
10 | This value is sometimes recommended to improve performance when sufficient memory exists in a system. |
60 | The default value. |
100 | The kernel will swap aggressively. Never use this! |
Set the value within the file /etc/sysctl.conf as vm.swappiness=10 (with the needed value)
Disk caching
sysctl -a | grep dirty vm.dirty_background_ratio = 10 vm.dirty_background_bytes = 0 vm.dirty_ratio = 20 vm.dirty_bytes = 0 vm.dirty_writeback_centisecs = 500 vm.dirty_expire_centisecs = 3000
- vm.dirty_background_ratio is the percentage of system memory that can be filled with “dirty” pages — memory pages that still need to be written to disk.
- vm.dirty_ratio is the absolute maximum amount of system memory that can be filled with dirty pages before everything must get committed to disk.
- vm.dirty_background_bytes and vm.dirty_bytes are another way to specify these parameters. If you set the _bytes version the _ratio version will become 0.
- vm.dirty_expire_centisecs is how long something can be in cache before it needs to be written. In this case it’s 30 seconds.
- vm.dirty_writeback_centisecs is how often the pdflush/flush/kdmflush processes wake up and check to see if work needs to be done.
Usecase: Fast, battery-backed storage
If you have a RAID controller with a battery you should decrease the disk-caching and let the controller handle the caching.
vm.dirty_background_ratio = 5 vm.dirty_ratio = 10
Usecase:
If your applications are writing to the same files repeatedly or in repeatable bursts.
vm.dirty_background_ratio = 50 vm.dirty_ratio = 80
Usecase: Infrequent, bursty traffic to slow disk like batch jobs
vm.dirty_background_ratio = 5 vm.dirty_ratio = 80
Create a swap file
To create an 1 GB SWAP-file use
dd if=/dev/zero of=/swapfile count=1024 bs=1MiB # or fallocate -l 1G /swapfile chmod 600 /swapfile mkswap /swapfile swapon /swapfile echo '/swapfile none swap sw 0 0' >> /etc/fstab
If you get "Swapon failed: Invalid argument" your filesystem probably does not support SWAP. You can still use the file if you really want by mounting it as loop device but this can result in very bad performance for the hole system and filesystem fragmentation. But this is a script to do it
#!/bin/sh -e path="/swapfile" dev=$(losetup -f) losetup "$dev" "$path" swapon "$dev"Source
Comments
Kaai
Article is unfinished - Work in progress!