8.2 内存与分配追踪
采样分配器的每一次 alloc/free,实时维护当前占用、历史峰值、按标签的分类占用,以及一份未释放清单。峰值与泄漏是两回事:峰值告诉你最坏时刻用多少,泄漏告诉你现在还欠谁。
本章与站内既有内容的分工
| 主题 | 站内既有内容 | 本节做什么 |
|---|---|---|
| 资源管理 | 《资产管线》 | 卷二管资产生命周期;本节追踪编辑器的内存分配 |
| 帧时间线 | 本卷 8.1 | 8.1 看时间;本节看内存,两者并列 |
| 全卷脉络 | 本卷目录 | 本节承接 Profiler 章 |
从零实现:内存与分配追踪
每次分配返回递增 id,记录大小与标签,并更新当前占用与峰值。释放时按 id 查明细,扣减当前占用后删除,重复释放返回 false。按标签汇总只统计仍在场的分配;泄漏清单即当前所有未释放条目。
对标采集与统计口径
Unreal Memory Insights 可追踪分配与释放,按调用栈或标签分析。helio 统计手动登记的分配。检查时仍未释放的条目可用于排查,是否泄漏取决于对象预期寿命。 Unreal Memory Insights
可运行产物:6 条用例
下面是 src/memory-tracking.ts,层级 L1,完整可编译。运行命令 node --test test/08-2-memory-allocation-tracking.test.ts:
// file: src/memory-tracking.ts
// tier: L1
// verify: node --test test/08-2-memory-allocation-tracking.test.ts
//
// 卷四(第八章):内存与分配追踪。
// 采样分配器的每一次 alloc/free,实时维护当前占用、历史峰值、按标签的
// 分类占用,以及一份未释放清单。峰值与泄漏是两回事:峰值告诉你最坏时刻
// 用多少,泄漏告诉你现在还欠谁。
export interface Allocation {
id: number;
size: number;
tag: string;
}
export class AllocTracker {
#live = new Map<number, Allocation>();
#seq = 0;
#current = 0;
#peak = 0;
alloc(size: number, tag: string): number {
this.#seq += 1;
this.#live.set(this.#seq, { id: this.#seq, size, tag });
this.#current += size;
if (this.#current > this.#peak) this.#peak = this.#current;
return this.#seq;
}
free(id: number): boolean {
const allocation = this.#live.get(id);
if (!allocation) return false;
this.#current -= allocation.size;
this.#live.delete(id);
return true;
}
current(): number {
return this.#current;
}
peak(): number {
return this.#peak;
}
byTag(): Record<string, number> {
const out: Record<string, number> = {};
for (const allocation of this.#live.values()) {
out[allocation.tag] = (out[allocation.tag] ?? 0) + allocation.size;
}
return out;
}
leaks(): Allocation[] {
return [...this.#live.values()].map((a) => ({ ...a }));
}
}
峰值只在分配时更新,释放时保留;这是「水位线」语义,不是当前值。按标签汇总只算在场分配,否则已经释放的纹理仍会出现在分类里,误导优化方向。泄漏清单返回副本,调用方无法篡改追踪器内部状态。
下面是 test/08-2-memory-allocation-tracking.test.ts,层级 L1:
// file: test/08-2-memory-allocation-tracking.test.ts
// tier: L1
// verify: node --test test/08-2-memory-allocation-tracking.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { AllocTracker } from '../src/memory-tracking.ts';
test('峰值记录最坏时刻占用', () => {
const t = new AllocTracker();
t.alloc(100, 'texture');
t.alloc(200, 'mesh');
assert.equal(t.current(), 300);
assert.equal(t.peak(), 300);
});
test('释放后当前占用下降但峰值保留', () => {
const t = new AllocTracker();
const id = t.alloc(100, 'texture');
t.free(id);
assert.equal(t.current(), 0);
assert.equal(t.peak(), 100);
});
test('按标签汇总当前占用', () => {
const t = new AllocTracker();
t.alloc(100, 'texture');
t.alloc(50, 'texture');
t.alloc(30, 'audio');
assert.deepEqual(t.byTag(), { texture: 150, audio: 30 });
});
test('未释放清单即泄漏候选', () => {
const t = new AllocTracker();
t.alloc(100, 'texture');
const second = t.alloc(30, 'audio');
t.free(second);
assert.deepEqual(t.leaks(), [{ id: 1, size: 100, tag: 'texture' }]);
});
test('重复释放被忽略', () => {
const t = new AllocTracker();
const id = t.alloc(10, 'x');
assert.equal(t.free(id), true);
assert.equal(t.free(id), false);
assert.equal(t.current(), 0);
});
test('空追踪器状态为零', () => {
const t = new AllocTracker();
assert.equal(t.current(), 0);
assert.equal(t.peak(), 0);
assert.deepEqual(t.byTag(), {});
assert.deepEqual(t.leaks(), []);
});
本机实测(Node v26.5.0,node --test test/08-2-memory-allocation-tracking.test.ts):tests 6 · pass 6 · fail 0,耗时约 40 ms。
前两条钉当前值与峰值;第三条钉标签汇总;第四条钉泄漏清单;后两条钉重复释放与空状态。
小结
- 峰值是历史水位线,释放不会降低它。
- 标签汇总是当前在场占用,不是累计分配量。
- 泄漏清单就是未释放集合,按 id 精确追踪。
- 重复释放是幂等的,必须显式返回 false。
阅读导航:上一节:8.1 CPU/GPU 采样与帧时间线 · 下一节:8.3 事件标记与性能标注 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。