Usage of the swap (default swappiness)

Default swappiness on my Rpi2 (1GB RAM) is set to 20 (default setting) using OSMC
PuTTY command: cat /proc/sys/vm/swappiness
Which will probably allow a better memory usage at the expense of more swap usage.

But a read something about that this could reduce the life of the sdcard,for comparison swappiness on my Rpi1 model B (512MB RAM) using raspbian is set to 1 (default setting)

Is there a reason why swappiness on Rpi2 is set so high by default ?

I wasn’t aware that the default value for Raspbian is 1, (I don’t think that has always been the case) but kernel default swapiness unless overridden is actually 70, so 20 is not a “high” setting in that context.

Remember also that Raspbian is a general purpose OS not specifically designed for media playback, while OSMC is optimised for Kodi media playback.

You might want to read through the following thread:

Granted it is optimising performance of a 256MB Pi, but the takeaway is that low values of swapiness can cause major stalls and stutter by causing the Kodi process to get stuck in uninterruptable sleep while the kernel tries to flush buffers/cache.

For a 256MB Pi, a swapiness of 20 is actually far to low (IMHO - Sam and I disagree on the optimum value as you will see in that thread :slight_smile: )

If you have plenty of free memory (as the Pi 2 does) then it won’t be swapping anyway, so the value of swapiness has no real effect.

All the swapiness value does is when the system is under memory pressure (not enough memory immediately available to satisfy a memory allocation) decide the balance of freeing buffers/cache versus swapping out to free up physical memory.

High values favour predominantly swapping out unused pages, low values favour predominately purging buffers/cache. (filesystem cache)

The problem with flushing buffers/cache is that pages in the cache can be locked by other processes, so the program requesting memory that isn’t immediately available (kodi) can be stalled for a long and potentially quite random amount of time while the kernel waits for other processes to release locks on the pages in the cache before those pages can be reclaimed.

This is acceptable for a general purpose OS but harms the realtime response of the OS for media playback.

High values of swapiness don’t necessarily mean a lot of disk writes unless you’re very low on memory - swapping out data pages requires disk writes however swapping out executable code pages doesn’t require any disk writes because the pages are already backed by the filesystem’s copy of the executable.

Eg if the portion of Kodi code that handles music playback doesn’t get used, those pages can be “swapped out” without actually writing to disk - they’re just dropped from memory and the resident set size (rsize) is reduced. If they are later needed they are paged back in directly from the executable file - so only reads are involved.

Another factor to consider is that SD cards are far more robust than they used to be - wear and tear on an SD card with a typical Kodi install just isn’t an issue these days now that all SD cards have good wear levelling.

In short, don’t sweat about the value of swappiness. :slight_smile:

It doesn’t matter. On a 512MB Pi a swap file does not even exist unless you manually created one: see