Operations 19 min read

Understanding Buffers and Cache in Linux Memory Management

This article explains the concepts, working principles, and system parameters of Linux buffers and cache, compares their roles in read/write operations, and provides practical vmstat and dd command examples to illustrate how they affect memory usage and I/O performance.

Deepin Linux
Deepin Linux
Deepin Linux
Understanding Buffers and Cache in Linux Memory Management

1. Overview of Buffer and Cache

In the world of computers, memory management is like a precise orchestra conductor, coordinating data flow and processing, which is decisive for system performance. Buffers and Cache are two crucial roles that work silently behind the scenes to improve efficiency, yet many people confuse them.

2. Buffer and Cache Working Principles

2.1 Buffer Working Principle

A buffer acts as a temporary staging area during data transfer between devices of different speeds, such as memory and disk. When writing data to disk, the data is first stored in the buffer; once the buffer reaches a certain size or meets write‑back conditions, the data is flushed to disk in larger chunks, reducing the number of I/O operations and improving throughput.

During reads, the disk first places data into the buffer, and the kernel then reads from the buffer, avoiding frequent slow disk accesses and ensuring stable, efficient data transfer.

Relevant system parameters:

(1) dirty_ratio

echo 20 > /proc/sys/vm/dirty_ratio
# or
sysctl -w vm.dirty_ratio=20

Purpose: defines the maximum percentage of dirty pages in memory before the system forces a synchronous write to disk.

Impact: controls timely write‑back of dirty pages, helping avoid excessive disk writes.

(2) dirty_background_ratio

echo 10 > /proc/sys/vm/dirty_background_ratio
# or
sysctl -w vm.dirty_background_ratio=10

Purpose: when the percentage of dirty pages exceeds this value, the kernel starts background write‑back, which does not block processes.

Impact: prevents premature write‑back, improving overall performance.

2.2 Cache: The Secret Weapon for System Acceleration

Cache works on the principle of locality: frequently accessed data is copied to a faster memory region. For example, database query results are stored in cache so that subsequent identical queries can be served instantly without re‑executing the query, dramatically reducing access time.

Cache typically uses replacement algorithms such as LRU (Least Recently Used) to decide which data to evict when space is limited, ensuring that the most likely to be accessed data stays in cache.

Relevant system parameters:

(1) vfs_cache_pressure

echo 100 > /proc/sys/vm/vfs_cache_pressure
# or
sysctl -w vm.vfs_cache_pressure=100

Purpose: adjusts the kernel's tendency to reclaim dentry and inode caches; higher values favor reclaiming dentries, lower values favor inode reclamation.

Impact: influences file‑system cache reclamation strategy and overall performance.

Example: swappiness

echo 10 > /proc/sys/vm/swappiness
# or
sysctl -w vm.swappiness=10

Purpose: controls the kernel's inclination to move pages to swap when memory is low; 0 means avoid swapping, 100 means prefer swapping.

Impact: lower values reduce swap usage and can improve overall performance.

3. Practical Case Analysis

3.1 Disk and File Write Case

Open a terminal and run vmstat 1 to monitor memory and I/O. The buff and cache columns correspond to Buffers and Cache (in KB), while bi and bo show block device read/write rates.

# Every second output one line
$ vmstat 1
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
0  0     0 7743608   1112  92168   0   0     0     0   52  152  0  1 100  0  0
...

Then, in another terminal, generate a 500 MB random file with dd if=/dev/urandom of=/tmp/file bs=1M count=500 and observe the changes in the first terminal. Cache grows continuously while Buffer stays almost unchanged.

Next, write directly to a raw disk partition (requires multiple disks and an unused /dev/sdb1 ) after clearing caches with echo 3 > /proc/sys/vm/drop_caches . Observe that both Buffer and Cache increase, but Buffer grows much faster, confirming that raw disk writes primarily use Buffer.

3.2 Disk and File Read Case

Clear caches, then read the previously created file with dd if=/tmp/file of=/dev/null . While reading, Buffer remains stable and Cache increases, matching the definition that Cache is the page cache for file reads.

Finally, read directly from a disk partition (e.g., /dev/sda1 ) after clearing caches. Both Buffer and Cache increase, but Buffer grows more rapidly, indicating that raw disk reads are cached in Buffer.

These experiments demonstrate that:

Buffer can cache data both for writing to disk and for reading from disk.

Cache can cache data both for reading files and for writing files.

In summary, Buffer is the cache for disk‑level I/O, while Cache is the cache for file‑level I/O, and both are involved in read and write operations.

CacheMemory ManagementPerformance TuningLinuxBufferddvmstat
Deepin Linux
Written by

Deepin Linux

Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.