-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleak-memory.js
69 lines (56 loc) · 1.55 KB
/
leak-memory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
'use strict';
const fs = require('fs');
const path = require('path');
const heapdump = require('heapdump');
// 模拟不会释放的内容,一直持有对象的引用
let neverRelease = [];
// 会释放的内容
let willRelease = [];
let usage = [];
// 模拟对象
class Foo {
constructor() {
this.id = (Math.random() + Date.now()).toString(36).substr(9);
}
}
function generatorData() {
let obj = new Foo();
neverRelease.push(obj);
willRelease.push(obj);
// 简单操作对象
doSomething(obj);
}
function doSomething(obj) {
obj.used = true;
// 用完清理数据
cleanUpData(obj);
}
function cleanUpData(obj) {
let index = willRelease.indexOf(obj);
// 清除掉 willRelease 里对对象的引用
// “忘了”清理 neverRelease 中的引用
willRelease.splice(index, 1);
// neverRelease.splice(index, 1);
// 一个对象,并且没被释放
// console.log(`[neverRelease] obj ${index} 是否使用过:${neverRelease[index].used}`);
}
function runtimeInfo() {
// 手动触发 GC
global.gc();
// 输出内存占用
let heapUsed = process.memoryUsage().heapUsed;
usage.push(heapUsed);
console.log(`堆占用 ${heapUsed} bytes.`);
// 触发生成 heapdump 文件
//heapdump.writeSnapshot(path.join(__dirname, `heapdump-${Date.now()}.heapsnapshot`));
process.kill(process.pid, 'SIGUSR2');
}
// 5ms 生成一次数据
setInterval(generatorData, 5);
// 2s 清理一次并打出占用
setInterval(runtimeInfo, 2000);
process.on('SIGINT', function(){
let data = JSON.stringify(usage);
fs.writeFileSync("usage.json", data);
process.exit();
});