↑ Top
Kaai24
dev studios
| Sign in | DE EN

Optimize SWAP and disk caching

for Linux


System performance is mostly affected by memory and disk IO so it should be configured to fit your usecase for best results.

Wikipedia
0

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
0

The most important SWAP option is the "swappiness" which defines a procentual value how it will be handled.

0Swap is disabled. Until Kernel 3.4, the kernel would swap only to avoid an out of memory condition.
1Kernel version 3.5 and over: Minimum amount of swapping without disabling it entirely.
10This value is sometimes recommended to improve performance when sufficient memory exists in a system.
60The default value.
100The 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
0

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

Usecase: Fast, battery-backed storage
0

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:
0

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
0

vm.dirty_background_ratio = 5
vm.dirty_ratio = 80

Create a swap file
0

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!