Site Tools


start:memmanagement

This is an old revision of the document!


On Linux systems, especially Debian some very old defaults are used. While this made sense 10-15 years ago, they are less optimal today, and changing the defaults is a very slow process since the logic is “if it aint broke don't fix it”. In some ways this is very useful as little changes, so you can expect a consistent experience between versions. However sometimes there is an issue. This is one of those times.

The default for Debian to start running kswapd (which compresses pages of memory and reclaims unused memory) is to wait for the system to have less than 80 Megabytes of memory left.

As you can imagine, many programs in 2023 have heavy memory requirements, even simple apps like web browsers, and many systems have 16GB or more RAM so waiting until only 80MB of memory is left does not seem like an optimal amount. When this is reached, the system must now perform extensive work reclaiming old pages and compacting them, while under memory pressure. This is most likely not an ideal time to do this, and will cause the “freezing” you notice while you wait for this to happen in the background. While doing this, new memory requests are placed in a queue until memory can be freed up, again which is not ideal. Most apps expect memory to be served on demand. All of these factors lead me to conclude setting more sane defaults is a logical step.

So instead of these defaults Debian ships with, we want to change them so that the system spends time earlier, ideally before all the memory is used up, backgrounding this process and avoiding the freezing a user may experience.

The 2 tweakable values we will deal with are:

  • vm.watermark_scale_factor
  • vm.min_free_kbytes

Both of these are edited in your sysctrl.conf file and placed at the end. To check your current values you would do something like:

cat /proc/sys/vm/watermark_scale_factor

Most likely a value of 10 is returned. This means 0.1% of memory. A value of 100 is 1% and a value of 1000 is 10%. Valid values are between 1-1000. The second value of min_free_kbytes default is 67584. This can be any value in KB.

To understand how kswapd decides how to use these value please review this graphic I have drawn in KolourPaint:

On the left is the default option. On the right an 8GB system that has had the optimizations I suggest is displayed.

The min value is calculated from the min_free_kbytes. That is about 64MB. The scale factor determines the low and high value by multiplying the system RAM by the scale factor and adding it to the min_free_kbytes. In our example that is 8192*0.1% = 8MB. 64MB is thus the minimum value, and 64+8 = 72MB for the low value and +8 again for 80MB which is the high value.

This determines that once the system falls below 72MB kswapd wakes up and starts trying to reclaim memory pages. It stops doing this when it gets back to 80MB. However as the next limit is very close (just 64MB) the possibility

start/memmanagement.1694719858.txt.gz · Last modified: 2023/09/14 19:30 by peter