Clash 内存优化与性能调优:让你的代理客户端跑得更轻
Clash 内存优化与性能调优:让你的代理客户端跑得更轻
很多 Clash 用户都有这样的体验:新装的 Clash Meta(Mihomo)跑得飞快,但用了几个月后内存占用从 80MB 涨到 800MB,规则匹配也开始肉眼可见地变慢。这是 Clash 的“通病”——但不是无解的。
本文从四个层面给出可落地的调优方案,让你的 Clash 客户端在低配电脑 / NAS / 软路由上也能流畅运行。
一、先看你的 Clash 当前状态
在动手优化前,先搞清楚“病根”在哪:
1.1 查看内存占用
Windows(任务管理器):
- Ctrl+Shift+Esc → 详细信息 → 找到
Clash for Windows.exe或mihomo.exe
macOS(活动监视器):
- Spotlight 搜索“活动监视器”→ 内存标签 → 找
Clash或Mihomo
Linux:
ps aux | grep -E "clash|mihomo" | grep -v grep
# 或更详细
top -p $(pgrep -f mihomo)
1.2 查看运行信息(API)
Clash Meta 的 External Controller 默认开启 127.0.0.1:9090,访问:
http://127.0.0.1:9090
可以看到实时内存、连接数、上传下载速率。
二、配置精简:让规则匹配从 3000 条降到 500 条
2.1 规则集过多的代价
Clash 每收到一个域名/IP,逐条匹配所有规则。一个 5000 条的 rules 文件,意味着每次 DNS 查询都要循环 5000 次。常见症状:
- 首次打开网页卡顿 1-3 秒
- 内存占用与规则数近似线性增长
- 软路由 CPU 长期 30%+
2.2 精简规则集方案
方案 A:使用 Loyalsoldier/loyalsoldier-clash-rules
这是社区维护的高质量精简规则集,按需启用:
rule-providers:
reject:
type: http
behavior: domain
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/reject.txt"
path: ./ruleset/reject.yaml
interval: 86400
icloud:
type: http
behavior: domain
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/icloud.txt"
path: ./ruleset/icloud.yaml
interval: 86400
private:
type: http
behavior: domain
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/private.txt"
path: ./ruleset/private.yaml
interval: 86400
gfw:
type: http
behavior: domain
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/gfw.txt"
path: ./ruleset/gfw.yaml
interval: 86400
greatcn:
type: http
behavior: domain
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/greatcn.txt"
path: ./ruleset/greatcn.yaml
interval: 86400
tld-not-cn:
type: http
behavior: domain
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/tld-not-cn.txt"
path: ./ruleset/tld-not-cn.yaml
interval: 86400
applications:
type: http
behavior: classical
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/applications.txt"
path: ./ruleset/applications.yaml
interval: 86400
rules:
- RULE-SET,applications,DIRECT
- DOMAIN,clash.razord.top,DIRECT
- DOMAIN,yacd.haishan.me,DIRECT
- RULE-SET,private,DIRECT
- RULE-SET,reject,REJECT
- RULE-SET,icloud,DIRECT
- RULE-SET,greatcn,DIRECT
- RULE-SET,tld-not-cn,PROXY
- RULE-SET,gfw,PROXY
- MATCH,PROXY
优势:
- 规则按类型拆分,按需加载
behavior: domain比 classical 节省 60%+ 内存- 总规则数从常见的 5000+ 降到约 2000 条
方案 B:合并 GeoIP/GeoSite
避免引入多个来源的 GeoIP/GeoSite 数据:
geosite:
cn: ./geosite.dat
google: ./geosite.dat
category-ads: ./geosite.dat
gfw: ./geosite.dat
geoip:
cn: ./country.dat
private: ./country.dat
确保 geosite.dat 和 country.dat 文件只加载一次。
三、缓存调优:DNS 与连接复用
3.1 DNS 缓存策略
Clash Meta 默认 DNS 缓存 5 分钟,但很多用户配置不当导致反复查询:
dns:
enable: true
ipv6: false
enhanced-mode: fake-ip # 推荐:Fake-IP 模式
fake-ip-range: 198.18.0.1/16
listen: 127.0.0.1:53
nameserver:
- https://1.1.1.1/dns-query
- https://8.8.8.8/dns-query
proxy-server-nameserver:
- https://1.1.1.1/dns-query # 代理域名用代理解析
direct-nameserver:
- 223.5.5.5 # 直连域名用国内 DNS
nameserver-policy:
"geosite:cn": 223.5.5.5
"geosite:gfw": https://1.1.1.1/dns-query
关键点:
- Fake-IP 模式比传统 redir-host 模式快 2-3 倍(无需每次查询真实 IP)
- 国内域名强制走国内 DNS,避免污染
enhanced-mode: redir-host模式在大量并发连接时容易 DNS 阻塞
3.2 连接复用配置
tcp-concurrent: true # TCP 并发连接,提升吞吐
mixed-port: 7890
allow-lan: true
bind-address: "*"
mode: rule
log-level: warning # 生产环境用 warning,info/warning 会写入大量日志
log-level: warning 比 info 日志量减少 90%+,内存占用明显下降。
四、内核参数调优
4.1 Linux / OpenWrt 系统参数
在 /etc/sysctl.conf 中添加:
# 增加端口范围(Clash 需要大量临时端口)
net.ipv4.ip_local_port_range = 10000 65000
# TCP 连接复用
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
# 文件描述符上限
fs.file-max = 2097152
fs.nr_open = 2097152
应用:
sysctl -p
4.2 OpenWrt 软路由专项
在 /etc/rc.local 中:
ulimit -n 65535
echo "net.core.default_qdisc=fq" >> /etc/sysctl.conf
echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
启用 BBR 拥塞控制,Clash 代理流量在拥塞链路上提升 30%+ 吞吐。
4.3 macOS 系统上限
sudo sysctl -w kern.maxfiles=65536
sudo sysctl -w kern.maxfilesperproc=32768
launchctl limit maxfiles 65536 32768
五、定期维护清单
每月做一次以下维护,Clash 可保持轻量运行:
| 维护项 | 频率 | 操作 |
|---|---|---|
| 重启 Clash 进程 | 每周 | kill -HUP $(pgrep -f mihomo) |
| 清理规则缓存 | 每月 | 删除 ~/.cache/clash/ 缓存目录 |
| 更新 GeoIP/GeoSite | 每月 | 重新下载最新版 dat 文件 |
| 更新订阅 | 每周 | 客户端“更新订阅”按钮 |
| 检查日志文件 | 每月 | 清理超过 100MB 的 log |
重启 Mihomo 的常用命令:
# Linux/macOS
systemctl restart mihomo # systemd 管理
kill -HUP $(pgrep mihomo) # 优雅重载配置
mihomo -d /etc/mihomo & # 直接重启
# Docker
docker restart mihomo
# OpenWrt
/etc/init.d/mihomo restart
六、低配设备特别优化(2GB RAM / 单核 CPU)
6.1 配置精简模板
mixed-port: 7890
allow-lan: false
mode: rule
log-level: error # 极致省资源
ipv6: false
dns:
enable: true
ipv6: false
enhanced-mode: fake-ip
listen: 127.0.0.1:53
nameserver:
- 223.5.5.5
- 114.114.114.114
# 仅保留必要规则
rules:
- GEOIP,CN,DIRECT
- MATCH,PROXY
6.2 关闭不用的特性
- 关闭 TUN 模式(仅用系统代理)
- 关闭
sniffer(TLS 嗅探) - 关闭
tcp-concurrent
6.3 老旧设备替代方案
| 设备 | 推荐 |
|---|---|
| 256MB RAM OpenWrt | 改用 PassWall / ShadowSocksR Plus+ |
| 树莓派 1/2 | 用 dnsmasq + ss-redir 替代完整 Clash |
| 32位 ARM | 编译 Mihomo 时启用 -tags with_gvisor=false |
七、内存监控与告警脚本
把以下脚本加入 crontab(Linux):
#!/bin/bash
# clash_memory_monitor.sh
THRESHOLD=500 # MB
PID=$(pgrep -f mihomo)
if [ -z "$PID" ]; then exit 0; fi
RSS=$(ps -p $PID -o rss= | awk '{print int($1/1024)}')
if [ "$RSS" -gt "$THRESHOLD" ]; then
echo "[$(date)] Mihomo memory usage: ${RSS}MB > ${THRESHOLD}MB, restarting..." >> /var/log/clash-monitor.log
kill -HUP $PID
fi
加入定时任务:
chmod +x clash_memory_monitor.sh
crontab -e
# 每 10 分钟检查一次
*/10 * * * * /path/to/clash_memory_monitor.sh
八、总结:Clash 调优核心口诀
规则要精、缓存要稳、日志要省、定期重启
| 调优点 | 收益 | 难度 |
|---|---|---|
| 规则集精简 | ⭐⭐⭐⭐⭐ | 低 |
| Fake-IP 模式 | ⭐⭐⭐⭐ | 低 |
| 日志降级 warning | ⭐⭐⭐ | 极低 |
| BBR 拥塞控制 | ⭐⭐⭐⭐ | 中 |
| Linux ulimit 调整 | ⭐⭐ | 低 |
| 定期重启 | ⭐⭐⭐⭐ | 极低 |
按本文步骤操作后,Clash Meta 的内存占用通常能从 600-800MB 降到 150-300MB,规则匹配速度提升 2-3 倍。即使在树莓派 4 这种 4GB RAM 的设备上,也能稳定运行数月无需重启。
常见问题 FAQ
Q1:Mihomo 比原版 Clash 省内存吗? A:是。Mihomo 修复了原版多个内存泄漏问题,长期运行更稳定。
Q2:Fake-IP 模式会影响 BT 下载吗?
A:可能。Peer 连接需要真实 IP,建议下载类流量配置为 DIRECT。
Q3:Clash Verge Rev 比 Clash for Windows 省内存吗? A:差异不大,主要是 GUI 进程占用。Mihomo 内核本身内存占用一致。
Q4:规则集越多越好吗? A:错。规则越多匹配越慢,500-1500 条规则足以覆盖 95% 场景。
Q5:怎么验证调优生效了? A:连续运行 7 天后对比 RSS 内存占用,理想情况下应稳定在 200MB 以内。
这篇教程有帮助吗?立即下载Clash入门试试吧!
全平台支持 · 安全无毒 · 官方最新版