Common Commands That Affect Cache in Linux Systems

In Linux systems, files are automatically cached in memory (Page Cache) when read. However, if you want to proactively warm up the cache (preload files into Cache), you can use the following methods:

## **1. Use the `dd` Command to Read Files (Trigger Cache)**

dd if=filename of=/dev/null bs=1M

– **Purpose**: Read the file without saving it, forcing Linux to cache it in memory.

– **Example** (Warm up `/var/log/syslog`):

dd if=/var/log/syslog of=/dev/null bs=1M

## **2. Use the `vmtouch` Tool (Specialized for Managing File Cache)**

`vmtouch` is a tool specifically designed for managing file cache, which can:

– **Check if a file is in the cache**

– **Proactively load a file into the cache**

– **Clear a file’s cache**

### **Install `vmtouch`**

# Debian/Ubuntu
sudo apt install vmtouch
# CentOS/RHEL
sudo yum install vmtouch

### **Common Commands**

| Function | Command |
|------|------|
| **Check if a file is in the cache** | `vmtouch -v filename` |
| **Proactively load a file into the cache** | `vmtouch -t filename` |
| **Clear a file from the cache** | `vmtouch -e filename` |

**Example** (Load `/usr/bin/python3` into cache):

vmtouch -t /usr/bin/python3

## **3. Use `cat` or `tail` to Read Files**

cat filename > /dev/null

– **Purpose**: Read the entire file to bring it into Page Cache.

– **Applicable Scenarios**: Suitable for small files; large files may be slower.

## **4. Use `find` + `xargs` to Warm Up Cache in Bulk**

find /path/to/files -type f -print0 | xargs -0 cat > /dev/null

– **Purpose**: Batch read all files in a directory into the cache.

## **5. Use `fincore` (Check File Cache Status)**

`fincore` can show which file blocks are cached:

fincore filename

**Example Output**:

filename: 16384 pages, 3 cached, 0% cached

– **3 cached** indicates that 3 memory pages are cached.

## **6. Use `mlock` (Lock Memory to Prevent Swapping)**

If you want to **keep a file in memory long-term** (prevent it from being swapped out), you can use `mlock`:

#include <sys/mman.h>
mlock(ptr, size);  // Lock memory
munlock(ptr, size); // Unlock

– **Applicable Scenarios**: Critical services (like databases) need to ensure certain files are always in memory.

## **Summary**

| Method | Applicable Scenarios | Notes |
|------|----------|------|
| `dd if=file of=/dev/null` | Manually warm a single large file | General method |
| `vmtouch -t file` | Precise cache control | Recommended |
| `cat file > /dev/null` | Small file caching | Simple but inefficient |
| `find + xargs cat` | Batch warm a directory | Suitable for multiple files |
| `fincore` | Check cache status | For debugging |
| `mlock` | Lock memory | Requires programming implementation |

**Recommended Methods**:

– **General Use** → `vmtouch` (most flexible)

– **Quick Testing** → `dd` or `cat`

– **Batch Operations** → `find + xargs`

These methods can help you optimize file access performance, especially in scenarios involving **databases, web services, and high-frequency access logs**.

Leave a Comment