教程

Claude Code 故障排查完整指南:高 CPU、卡死、自动压缩震荡和搜索失效

Claude Code 官方 Troubleshooting 完整中文整理:高 CPU/内存占用、卡死或冻结、自动压缩 thrashing、搜索不到文件、WSL 搜索结果不完整、ripgrep 替换、/doctor 自动诊断、/heapdump 内存快照、/compact 精准压缩、/clear 清空上下文、subagent 分离大文件任务,以及安装/登录/API/IDE 问题应该去哪些页面排查。

2026/5/155分钟 阅读ClaudeEagle

Claude Code 出问题时,不要盲目重装。官方 Troubleshooting 文档把问题分成几类:安装登录、配置、API 错误、IDE 集成、性能稳定性和搜索问题。本文聚焦 Claude Code 已经能运行后的性能和稳定性排查。


先用 /doctor 定位问题

如果 Claude Code 能启动,在会话内运行:

text
/doctor

它会自动检查:

  • 安装健康状态
  • settings 配置
  • MCP server 状态
  • 上下文占用
  • 搜索能力

如果 claude 根本启动不了,在 shell 里运行:

bash
claude doctor

问题导航表

症状去哪里排查
command not found、安装失败、PATH、EACCES、TLSTroubleshoot installation and login
Login loop、OAuth、403、organization disabled、Bedrock/VertexLogin and authentication
settings 不生效、hooks 不触发、MCP 不加载Debug your configuration
API Error 5xx、529、429、request validationError reference
model not foundError reference 的 model 部分
VS Code extension 不连接VS Code integration
JetBrains plugin 不检测JetBrains integration
高 CPU/内存、慢、卡死、搜索异常本文下方处理

高 CPU 或内存占用

Claude Code 能处理大多数开发环境,但在大仓库或长会话中可能占用较多资源。

官方建议:

  1. 定期运行 /compact
  2. 大任务之间关闭并重启 Claude Code
  3. 把大型构建目录加入 .gitignore

常见应该加入 .gitignore 的目录:

gitignore
node_modules/
dist/
build/
.next/
.nuxt/
coverage/
.pytest_cache/
.venv/
target/

如果内存仍然很高,运行:

text
/heapdump

它会在 ~/Desktop 写出 JavaScript heap snapshot 和 memory breakdown。Linux 没有 Desktop 时,写到 home 目录。

memory breakdown 会显示:

  • resident set size
  • JS heap
  • array buffers
  • unaccounted native memory

.heapsnapshot 可用 Chrome DevTools 的 Memory → Load 打开,分析保留对象。


Auto Compact Thrashing 怎么办?

错误类似:

text
Autocompact is thrashing: the context refilled to the limit...

意思是:自动压缩成功了,但某个文件或工具输出立刻又把上下文窗口填满,反复几次后 Claude Code 停止自动重试,避免浪费 API 调用。

解决方案:

1. 让 Claude 分块读取大文件

不要说:

text
读取这个 5MB 日志并分析

改成:

text
只读取 error.log 中包含 "FATAL" 的行,或者读取 1200-1450 行之间的 stack trace

2. 用带目标的 /compact

text
/compact keep only the plan and the diff

明确告诉 Claude 保留什么,丢弃无关的大输出。

3. 把大文件任务交给 Subagent

text
把这个大日志分析任务交给 subagent,在独立上下文里完成,只把结论带回来

Subagent 有独立上下文窗口,不会污染主会话。

4. 如果历史不重要,直接 /clear

text
/clear

适合任务已经完成、旧上下文没有价值的情况。


Claude Code 卡死或冻结

处理顺序:

  1. Ctrl+C 尝试取消当前操作
  2. 如果仍无响应,关闭终端并重启
  3. 在同一目录运行恢复:
bash
claude --resume

重启不会丢失对话,--resume 可以继续之前的 session。


搜索不到文件、@file 不工作、Skills/Agents 不发现

如果 Search 工具、@file、custom agents 或 custom skills 找不到文件,可能是内置 ripgrep 在你的系统上无法运行。

安装系统版 ripgrep:

macOS:

bash
brew install ripgrep

Ubuntu/Debian:

bash
sudo apt install ripgrep

Alpine:

bash
apk add ripgrep

Arch:

bash
pacman -S ripgrep

Windows:

powershell
winget install BurntSushi.ripgrep.MSVC

然后设置:

bash
export USE_BUILTIN_RIPGREP=0

这样 Claude Code 会使用系统安装的 ripgrep。


WSL 搜索慢或结果不完整

如果项目位于 /mnt/c/ 这类 Windows 文件系统路径,WSL 的磁盘读性能会影响搜索,导致结果比原生文件系统少。

解决办法:

  1. 更具体地搜索:限制目录和文件类型
  2. 把项目移动到 Linux 文件系统,例如 /home/you/project
  3. 改用原生 Windows Claude Code

示例 Prompt:

text
在 auth-service 包里搜索 JWT validation 逻辑,只看 TypeScript 文件

比:

text
搜索 JWT

更可靠。


实战排查流程

情况 1:Claude 变慢

text
/doctor
/context
/compact keep only current task plan and modified files

然后检查仓库是否有超大目录没被 ignore。

情况 2:自动压缩反复失败

  1. 找出最近读入的大文件/大输出
  2. /compact keep only the plan and the diff
  3. 后续让 Claude 分块读取
  4. 大日志/大 JSON 交给 subagent

情况 3:搜索异常

  1. /doctor 看 Search 状态
  2. 安装系统 ripgrep
  3. 设置 USE_BUILTIN_RIPGREP=0
  4. WSL 用户把项目移到 /home/

来源:Claude Code 官方文档 - Troubleshooting | 整理:ClaudeEagle

相关文章推荐

教程深入理解 Claude Code 上下文窗口:每个操作消耗多少 Token,一目了然深入分析 Claude Code 200K 上下文窗口的使用方式:每个操作消耗多少 Token、什么被自动加载、如何优化上下文使用。含 7 个实用优化技巧。2026/4/7教程Claude Code Fast Mode 详解:Opus 4.6 提速 2.5 倍的正确打开方式Claude Code Fast Mode 让 Opus 4.6 响应速度提升 2.5 倍。详解开启方式、定价策略、适用场景,以及与 Effort Level 的组合使用技巧。2026/4/7教程Claude Code .claudeignore 完全指南:精准控制 AI 读取文件的范围Claude Code .claudeignore 文件完整使用指南:语法规则(与 .gitignore 完全一致)、为什么需要排除文件(隐私/性能/干扰)、推荐排除的文件类型(node_modules/secrets/.env/构建产物)、按项目类型的最佳实践配置(Node.js/Python/Go/单体仓库)、.claudeignore 与 .gitignore 的区别,以及如何验证排除规则是否生效。2026/3/18教程Claude Code 报错解决大全:连不上/登录失败/命令找不到等 20 个常见问题Claude Code 常见问题排查完整指南:无法连接到 claude.ai、OAuth 登录失败、命令找不到、API 认证错误、超出上下文限制、输出中断、文件权限拒绝、Git 集成失败等 20+ 报错的具体原因分析和解决步骤,适用于 macOS/Windows/Linux 三平台。2026/3/16教程Claude Code 常见错误解决大全:安装失败、认证问题、性能卡顿 22 个解法Claude Code 故障排查手册:command not found、OAuth 错误、403 Forbidden、Hooks 不触发、WSL2 问题等常见故障的直接可用解决命令,按安装/认证/性能/IDE/配置/WSL2/重置七类分组。2026/3/14教程Claude Code 安装故障排查:15 种常见报错的逐一修复方法Claude Code 安装故障排查完整指南:15 种常见报错症状对照表(command not found/TLS错误/HTML返回/Killed/musl不匹配/架构不匹配/Git Bash未配置等)、安装前诊断(Google Cloud Storage 连通性/代理配置/目录权限)、六大详细问题(PATH 修复/HTML 脚本返回/TLS CA 证书/Alpine musl/多版本冲突/低内存 Killed)各平台命令,以及 claude doctor 运行时诊断和认证问题修复。2026/3/7