When examining system memory usage with the free
command, you'll notice two important categories: buffers and cache. These represent different aspects of how Linux manages memory for optimal performance.
$ free -m
total used free shared buff/cache available
Mem: 7982 1843 3521 123 2617 5716
Swap: 2047 0 2047
Buffers represent memory allocated by the kernel to temporarily store data being transferred between processes and block devices (like disks). They serve as:
- Intermediate storage for raw disk blocks
- Temporary holding space for metadata (inode tables, superblocks)
- Write-behind caching for filesystem operations
Example scenario where buffers are used:
# When writing a large file
$ dd if=/dev/zero of=testfile bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 5.12345 s, 205 MB/s
# Check buffer usage before and after
$ free -m | grep -i buff
Cache memory stores frequently accessed data from disks to accelerate future requests. It includes:
- Page cache for file contents
- Directory entries cache
- Inode caches
Example showing cache behavior:
# First read (populates cache)
$ time cat largefile > /dev/null
real 0m5.123s
# Second read (from cache)
$ time cat largefile > /dev/null
real 0m0.456s
Characteristic | Buffers | Cache |
---|---|---|
Purpose | Temporary storage for metadata and raw blocks | Accelerated access to file contents |
Management | Block layer | Filesystem layer |
Content Type | Raw disk blocks | File pages |
Flush Behavior | Written to disk | Can be discarded |
Understanding these concepts helps with:
- Performance Tuning: Know when to drop caches (
echo 3 > /proc/sys/vm/drop_caches
) - Memory Monitoring: Properly interpret memory usage statistics
- Application Design: Optimize I/O patterns based on caching behavior
For deeper analysis, use these tools:
# Detailed slab info (includes cache allocations)
$ sudo slabtop
# Page cache statistics
$ grep -i page /proc/meminfo
# Buffer cache details
$ sudo hdparm --fibmap /path/to/file
When examining Linux system memory usage through the free -m
command, you'll encounter three important memory categories:
total used free shared buffers cached Mem: 7986 7842 144 0 248 3714 -/+ buffers/cache: 3879 4107 Swap: 2047 6 2041
Buffers represent temporary storage for raw disk blocks that haven't yet been written to disk. This occurs when:
- Processes perform write operations
- Filesystem metadata needs to be updated
- The kernel queues I/O operations
Example scenario where buffers accumulate:
# Large file copy operation dd if=/dev/zero of=testfile bs=1M count=1024
Cache contains data read from disks that remains in memory for potential future use. This includes:
- Recently accessed files
- Frequently used program binaries
- Directory structures
To demonstrate cache behavior:
# First read (populates cache) time cat large_file > /dev/null # Subsequent reads (from cache) time cat large_file > /dev/null
Understanding the distinction helps with:
# Clearing buffers (requires root) sync; echo 1 > /proc/sys/vm/drop_caches # Monitoring buffer/cache usage vmstat 1
Key considerations for system tuning:
- Servers with frequent writes need buffer space
- Read-heavy systems benefit from larger caches
- The kernel automatically reclaims both when needed
Adjusting kernel parameters (in /etc/sysctl.conf):
# Control cache behavior vm.vfs_cache_pressure=50 vm.swappiness=10