> ## Documentation Index
> Fetch the complete documentation index at: https://bun.ll1025.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 V8 堆快照检查内存使用情况

Bun 实现了 V8 的堆快照 API。使用它在运行时捕获堆，并调试 JavaScript/TypeScript 应用程序中的内存泄漏。

```ts snapshot.ts icon="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/icons/typescript.svg?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=787d39d6a7d96d7f9540dc74344eba23" theme={null}
import v8 from "node:v8";

// 创建堆快照文件，文件名自动生成
const snapshotPath = v8.writeHeapSnapshot();
console.log(`堆快照已写入：${snapshotPath}`);
```

***

## 在 Chrome DevTools 中检查内存

要在 Chrome DevTools 中查看 V8 堆快照：

1. 打开 Chrome DevTools（F12 或右键点击选择"检查"）
2. 进入"Memory"（内存）选项卡
3. 点击"Load"（加载）按钮（文件夹图标）
4. 选择你的 `.heapsnapshot` 文件

<Frame>
  <img src="https://mintcdn.com/span-inc/82N53aP7NFbaCVSl/images/chrome-devtools-memory.png?fit=max&auto=format&n=82N53aP7NFbaCVSl&q=85&s=1be02493abf83815b536fedb90473114" alt="Chrome DevTools 内存选项卡" width="1770" height="1201" data-path="images/chrome-devtools-memory.png" />
</Frame>
