Which Data Occupies Linux Cache

In Linux systems, the memory management mechanism utilizes free memory to cache various data to improve performance. Below is a detailed analysis of whether different components occupy **Cache** (cached memory):

## **1. Relationship Between Memory and Cache**

– **File System Cache (Cached)**: Linux actively caches read files in memory (i.e., `Cached`) to speed up subsequent access.

– **Anonymous Memory (AnonPages)**: Memory such as application heaps and stacks does not count towards `Cached`, but may be swapped out.

– **Buffers**: Primarily used for temporary data caching of block devices (like disks), which is different from `Cached` but still considered reclaimable memory.

**Conclusion**: Ordinary memory data (like process memory) does not count towards `Cached`, but file read/write operations will occupy `Cached`.

## **2. Does Network Data Occupy Cache?**

– **Received/Sent Network Data**:

– **TCP Receive Buffer (`sk_buff`)**: The kernel allocates memory for network packets, which belongs to dynamic memory and does not directly count towards `Cached`.

– **NIC (Network Interface Card) Cache**: Managed by hardware, does not occupy system `Cached`.

– **Application Reading Network Data**: If data is written to a file (like a downloaded file), subsequent reads of that file will occupy `Cached`.

**Conclusion**: Network data itself does not directly count towards `Cached`, but if the data is written to a file and then read again, it will occupy `Cached`.

## **3. Does Disk Data Occupy Cache?**

– **Reading Disk Files**: All read file contents will be cached in `Cached` (unless using `O_DIRECT` to bypass the cache).

– **Writing Disk Files**: Data written may first be cached in memory (as `Dirty` memory) and later written to disk by `pdflush` or `fsync`.

– **Direct I/O**: Databases (MySQL, PostgreSQL) may bypass `Cached` and directly read/write to disk.

**Conclusion**: Ordinary disk read/write operations will occupy `Cached`, unless explicitly bypassing the cache.

## **4. Does NFS (Network File System) Occupy Cache?**

– **NFS Client Cache**:

– Read NFS files will be cached in `Cached` (similar to local files).

– `NFS_Unstable` indicates data that has been written but not committed to the NFS server.

– **NFS Server Cache**:

– The server side also caches file data (`Cached`).

**Conclusion**: NFS file access will occupy `Cached`, similar to local file systems.

## **5. Do Telnet/SSH Sessions Occupy Cache?**

– **SSH/Telnet Session Data**:

– Terminal data transmission (like `ssh`, `telnet`) does not directly cache to `Cached`.

– However, if the transmitted files are written to disk, they will be cached upon subsequent reads.

– **SSH Authentication Related Files**:

– Files like `/etc/passwd`, `/etc/shadow`, `authorized_keys`, etc., if read, will enter `Cached`.

**Conclusion**: SSH/Telnet session data does not directly count towards `Cached`, but file access involved will be cached.

## **Summary: What Situations Occupy `Cached`?**

| **Component**       | **Occupies `Cached`** | **Description** ||--------------|------------------|--------|| Ordinary Memory (Process Heap, Stack) | ❌ Does not occupy | Belongs to `AnonPages` || File Read/Write (Disk, NFS) | ✅ Occupies | File content cached || Network Data (Pure Transmission) | ❌ Does not occupy | Unless written to file and read again || NFS File Access    | ✅ Occupies | Similar to local file cache || SSH/Telnet Session | ❌ Does not occupy | Unless involving file access || Database (Direct I/O) | ❌ Does not occupy | Bypasses cache |

## **How to Check Current `Cached` Memory?**

```bashcat /proc/meminfo | grep Cached```

or

```bashfree -h```

(The `buff/cache` column in the `free` command includes `Buffers` + `Cached`)

## **How to Clear `Cached` Memory?**

```bashsync  # Ensure data is written to diskecho 1 > /proc/sys/vm/drop_caches  # Clear PageCacheecho 2 > /proc/sys/vm/drop_caches  # Clear Slab + Inodeecho 3 > /proc/sys/vm/drop_caches  # Clear all caches```

(**Note**: This is a temporary clear; the system will automatically re-cache data.)

### **Key Conclusions**

– **`Cached` is primarily file cache**, disk and NFS file access will occupy it.

– **Network data, SSH/Telnet do not directly cache**, unless involving file read/write.

– **Linux automatically manages `Cached`**, usually no manual clearing is needed unless for special requirements (like testing memory performance).

Leave a Comment