# alinux-system

Part of **ALINUX**

# Alibaba Cloud Linux System Performance CLI Reference

## Command Overview

| Command | Purpose | Syntax |
|--------|--------|--------|
| `echo` with `/proc/sys/vm/drop_caches` | Clear page cache, dentries, and/or inodes | `sudo sh -c 'echo [1\|2\|3] > /proc/sys/vm/drop_caches'` |
| `sysctl -w vm.drop_caches` | Clear kernel caches via sysctl interface | `sudo sysctl -w vm.drop_caches=[1\|2\|3]` |

## Command Details

### Clear Page Cache via `/proc/sys/vm/drop_caches`

**Purpose**: Free unused memory by clearing page cache, directory entries (dentries), and inode caches. This helps reclaim memory without killing processes.

**Syntax**:
```bash
sudo sh -c 'echo N > /proc/sys/vm/drop_caches'
```

| Parameter | Short | Type | Required | Description |
|----------|-------|------|----------|-------------|
| N (value written) | — | integer | Yes | Specifies what to clear: `1` = page cache only, `2` = dentries and inodes only, `3` = both page cache and dentries/inodes |

```bash
# Clear only page cache (safe, non-destructive)
sudo sh -c 'echo 1 > /proc/sys/vm/drop_caches'

# Clear dentries and inodes only
sudo sh -c 'echo 2 > /proc/sys/vm/drop_caches'

# Clear all three: page cache, dentries, and inodes
sudo sh -c 'echo 3 > /proc/sys/vm/drop_caches'
```

**Output Example**:
```text
(no output on success; command completes silently)
```

> **Note**: Always run `sync` before clearing caches to flush dirty pages to disk and avoid data loss.

### Clear Caches via `sysctl`

**Purpose**: Same as above, but uses the `sysctl` utility for a more standardized interface.

**Syntax**:
```bash
sudo sysctl -w vm.drop_caches=N
```

| Parameter | Short | Type | Required | Description |
|----------|-------|------|----------|-------------|
| `--vm.drop_caches` | — | integer | Yes | Value `1`, `2`, or `3` with same meaning as above |

```bash
# Clear page cache using sysctl
sudo sysctl -w vm.drop_caches=1

# Clear all caches (page cache + dentries/inodes)
sudo sysctl -w vm.drop_caches=3
```

**Output Example**:
```text
vm.drop_caches = 1
```

## Common Scenarios

### Scenario 1: Free Memory Before Running a Memory-Intensive Job
```bash
# Step 1: Ensure all pending writes are flushed to disk
sync

# Step 2: Clear page cache to maximize available memory
sudo sh -c 'echo 1 > /proc/sys/vm/drop_caches'
```

### Scenario 2: Reset Filesystem Metadata Caches After Large File Operations
```bash
# Step 1: Flush disk buffers
sync

# Step 2: Clear dentries and inodes to release metadata memory
sudo sysctl -w vm.drop_caches=2
```

## Environment Setup

### Installation
No additional installation is required. The `/proc/sys/vm/drop_caches` interface and `sysctl` are built into the Linux kernel and available by default on Alibaba Cloud Linux 2 and 3.

### Configuration
- Requires `root` privileges or membership in a group with `sudo` access to write to `/proc/sys/vm/drop_caches`.
- No persistent configuration is needed—these are runtime-only operations.
- To make cache clearing behavior persistent across reboots (not recommended), add settings to `/etc/sysctl.conf`, but note that `vm.drop_caches` is not meant to be set persistently.

## FAQ

Q: Do I need to run `sync` before clearing caches?
A: Yes. Running `sync` ensures all dirty pages are written to disk. Without it, clearing caches could result in data loss if applications have unwritten changes in memory.

Q: What is the difference between values 1, 2, and 3 for `drop_caches`?
A: Value `1` clears only the page cache (file data). Value `2` clears directory entries (dentries) and inode caches (metadata). Value `3` clears both, which is equivalent to `1 + 2`.

Q: Will clearing caches improve system performance?
A: Only in specific cases—such as when the system is under memory pressure and cached data is no longer useful. In normal operation, the kernel manages cache efficiently, and manual clearing may reduce performance by discarding useful cached data.

Q: Can this command cause system instability?
A: No, because it only frees *reclaimable* memory (clean caches). It does not touch application memory or dirty pages (if `sync` is used first). However, frequent use may indicate an underlying memory sizing or application issue.

Q: Why does `sysctl -w vm.drop_caches=1` show output while the `echo` method does not?
A: The `sysctl` command echoes the applied setting by design for confirmation. The `echo` method writes directly to a virtual file and produces no output unless an error occurs.