Linux – Reserve RAM for cache and buffer

linuxmemoryswap

I usually run many programs and virtual machines on my desktop. The CPU isn't the bottleneck but the memory. (Buying more memory isn't a solution because it's not my own computer.)

I started experimenting with compcache and it works fine on compressing memory. I can increase it's usage by swappiness 100. But when real (uncompressed), memory starts to fill up then Linux starts to empty caches and buffers before massive swapping what is bad for me because switching between programs gets painfully slow.

I wasn't able to find a way to reserve memory for buffers and caches so I started to look alternate ways and found cgroups.

In cgroups you can't set memory limit for root resources so I tried to move all processes into a group with limited memory, but somehow it didn't seems working.

Please help how can I reserved memory for buffers and caches and start swapping to compcache at an early stage.

Best Answer

If you want to reserve memory for cache and buffer: echo 10 > /proc/sys/vm/vfs_cache_pressure when 100 is the default value. Then you can limit the max ram used by each app: echo 8192 > /proc/sys/vm/max_map_count. I recommend a swapiness=30 and high /proc/sys/vm/dirty_writeback_centisecs and /proc/sys/vm/dirty_expire_centisecs values (both: 1250). It can also help to tweak the file system:

echo 8192 > /sys/block/mmcblk0/queue/max_sectors_kb
echo 8192 > /sys/block/mmcblk1/queue/max_sectors_kb
echo 0 > /sys/block/mmcblk0/queue/iosched/slice_idle
echo 0 > /sys/block/mmcblk1/queue/iosched/slice_idle
echo 160 > /sys/block/mmcblk0/queue/iosched/quantum
echo 160 > /sys/block/mmcblk1/queue/iosched/quantum
echo 800 > /sys/block/mmcblk0/queue/iosched/fifo_expire_sync
echo 800 > /sys/block/mmcblk1/queue/iosched/fifo_expire_sync
echo 180 > /sys/block/mmcblk0/queue/iosched/fifo_expire_async
echo 180 > /sys/block/mmcblk1/queue/iosched/fifo_expire_async
echo 1 > /sys/block/mmcblk0/queue/iosched/back_seek_penalty 
echo 1 > /sys/block/mmcblk1/queue/iosched/back_seek_penalty

These values are from my mobile and my laptop and it works really stable and makes things pretty fast. My mobile is memory limited but this tweaks helps a lot. My mobile has cgroups enabled too and I would recommend the 4k wonderpatch because this adds custom cgroups to each app a user is running from the terminal (not desktop) but I don't know how to limit memory with cgroups.