Konubinix' opinionated web of thoughts

How to Configure File System Caching in Linux | Baeldung on Linux

Fleeting

How to Configure File System Caching in Linux | Baeldung on Linux

file system cache holds data recently read from secondary storage. This makes it possible for subsequent requests to obtain data from the cache instead of reading it from slower memory

https://www.baeldung.com/linux/file-system-caching

store frequently accessed data in memory for faster access

https://www.baeldung.com/linux/file-system-caching

kernel uses the page cache to store recently-read data from files and file system metadata.

https://www.baeldung.com/linux/file-system-caching

when a program reads data from a file, the kernel performs several tasks:

checks the page cache to see if the data is already in memory if the data is in memory, the kernel simply returns the data from the cache otherwise, it reads the data from the drive and stores a copy of it in the cache for future use

https://www.baeldung.com/linux/file-system-caching

he vmstat command provides detailed information about virtual memory. In particular, it shows the amount of memory in use for caching: $ vmstat

https://www.baeldung.com/linux/file-system-caching

cache column shows the amount of memory used for file system caching in kilobytes

https://www.baeldung.com/linux/file-system-caching

use the free command to check the amount of file system cache memory in the system. It shows the memory usage in kilobytes under the buff/cache column

https://www.baeldung.com/linux/file-system-caching

value of the buff/cache column is the sum of the values of the buffer memory and swap cache rows for vmstat.

https://www.baeldung.com/linux/file-system-caching

value of vm.vfs_cache_pressure, which controls the tendency of the kernel to reclaim the memory used for caching directory and inode objects:

https://www.baeldung.com/linux/file-system-caching

Swappiness controls how aggressively the kernel swaps memory pages. Lowering the value of swappiness means the kernel will be less likely to swap out less frequently used memory pages. Thus, the kernel will be more likely to keep these pages cached in RAM for faster access.

https://www.baeldung.com/linux/file-system-caching

dirty pages are memory pages that aren’t written to secondary memory yet.

https://www.baeldung.com/linux/file-system-caching

vm.dirty_background_ratio parameter is the amount of system memory in percentage that can be filled with dirty pages before they’re written to the drive

https://www.baeldung.com/linux/file-system-caching

For instance, if we set the value of the vm.dirty_background_ratio parameter of a 64GB RAM system to 10, it entails that 6.4GB of data (dirty pages) can stay in RAM before they’re written to the storage.

https://www.baeldung.com/linux/file-system-caching

Alternatively, we can set the vm.dirty_background_bytes variable in place of vm.dirty_background_ratio. The *_bytes version takes the amount of memory in bytes. For example, we can set the amount of memory for dirty background caching to 512MB

https://www.baeldung.com/linux/file-system-caching

the *_ratio variant will become 0 if we set the * _bytes variant, and vice versa

https://www.baeldung.com/linux/file-system-caching

vm.dirty_ratio is the absolute maximum amount of system memory in percentage that can be filled with dirty pages before they’re written to the drive. At this level, all new I/O activities halt until dirty pages are written to storage.

https://www.baeldung.com/linux/file-system-caching

vm.dirty_expire_centisecs manages how long data can be in the cache before it’s written to drive. Let’s set the variable so that data can stay for 40 seconds in the cache:

https://www.baeldung.com/linux/file-system-caching

40 seconds before it’s written to the drive. Notably, 1s equals 100 centisecs.

https://www.baeldung.com/linux/file-system-caching

vm.dirty_writeback_centisecs is the variable for how often the write background process checks to see if there’s data to write to secondary storage.

https://www.baeldung.com/linux/file-system-caching