Mem usage and cache/swapping behavior

I am using fedora workstation in a virtual machine as well and was wondering why the system becomes very slow while working on it. After hours of investigation, I found the main reason:

The newer linux kernels using the fs cache intensive. Which is a nice idea, since unused mem is wasted mem. Unfortunately, if my programm reads a lot of files, the fs cache will be increased above the available physical mem. e.g. I am using 3.2GB of mem and the cache consumes 6.2GB which is a total of 9.2 GB. The system has only 8GB available and swapping kicks in with 1.2GB.

Since swap space is the slowest mem device we can get, the system should only swap if really needed. That means, the system should decrease the cache to a minimum before using swapping. - It seems to swap the cache mem as well

I found no kernel or fs parameter to fine tune this. But this is the reason, why many system owner having trouble with newer kernel versions using the same programms as before.

Is there a way to fine tune this or to control this in a kernel build?

That should not happen as these cached blocks should just be dropped when the space is needed for other purposes. The data can always be fetched from the disk file again if needed. Cached block from files you are writing to must be written back to the disk file, and when that is done, the cached block can be dropped.

The read-only part of a program can also be dropped as that too can be retrieved from the program file. Only the data portion of the program will need to be written to the swap device when the space is needed. The data you write to /tmp may also be written to the swap device, as there is nowhere else it can be saved.

1 Like

“should not happen”, “should just be dropped” and “can” - I already wrote that this happends

I tested again several settings and switched off the swap temporarely and came to the same result: That implemented cache logic does not fit for a “used” system.

I found the option “Use Host I/O-Cache” in VirtualBox and the swapping is gone. The cache size is not decreased but the system is now performing as expected.

Ok, found it:

This issue has nothing to do with the fs cache. It is rather an application issue. I am using Visual Studio Code and java and the java mem setting was too high. That means, that the java process reserves buffer which is not released in time. E.g. the process mem was < 500MB but the cache by 8GB.

Even a force to drop cache cannot solve such issues since the process has allocated that mem.

The java garbage collector is constantly scanning all the heap memory, so it is important that the maximum heap memory is chosen so it all fit into real memory without ever being swapped. If parts of the heap memory is swapped out, it will almost immediately be swapped back in again. Java processes never released heap memory back to the operating system once that memory has been allocated. Java just uses its internal memory management to constantly allocate and release memory when creating objects and when these objects are later garbage collected.