Mongo调用导致Node js内存泄漏



当我运行返回 22k 文档的await Order.find({})时,used_heap_size从大约 50mb 上升到 430mb 左右。

它似乎永远不会回落。

used_heap_size不应该很快回去吗?我不确定这是否被定义为内存泄漏,但似乎是这样。这是什么原因造成的?这正常吗?

app.get('/orders', async (req, res, next) => {
v8Print('before find orders');
const j = await Order.find({});
v8Print('afters find orders');
res.sendStatus(200);
});
app.get('/logMem', async (req, res, next) => {
v8Print('memory info');
res.sendStatus(200);
});

日志功能

const v8 = require('v8');
// for memory allocation heap debugging
module.exports.v8Print = (message) => {
const heapStats = v8.getHeapStatistics();
const heapStatsMB = heapStats;
for (const key in heapStatsMB) {
heapStatsMB[key] = `${(((heapStatsMB[key] / 1024 / 1024) * 100) / 100).toFixed(2)} MB`;
}
console.log('');
console.log(message);
console.table(heapStatsMB);
console.log('');
};
before find orders
┌─────────────────────────────┬──────────────┐
│           (index)           │    Values    │
├─────────────────────────────┼──────────────┤
│       total_heap_size       │  '54.05 MB'  │
│ total_heap_size_executable  │  '0.80 MB'   │
│     total_physical_size     │  '53.71 MB'  │
│    total_available_size     │ '1999.10 MB' │
│       used_heap_size        │  '49.56 MB'  │
│       heap_size_limit       │ '2048.00 MB' │
│       malloced_memory       │  '0.01 MB'   │
│    peak_malloced_memory     │  '2.48 MB'   │
│      does_zap_garbage       │  '0.00 MB'   │
│  number_of_native_contexts  │  '0.00 MB'   │
│ number_of_detached_contexts │  '0.00 MB'   │
└─────────────────────────────┴──────────────┘

afters find orders
┌─────────────────────────────┬──────────────┐
│           (index)           │    Values    │
├─────────────────────────────┼──────────────┤
│       total_heap_size       │ '521.66 MB'  │
│ total_heap_size_executable  │  '1.05 MB'   │
│     total_physical_size     │ '520.90 MB'  │
│    total_available_size     │ '1611.12 MB' │
│       used_heap_size        │ '435.79 MB'  │ // <---435 after Orders.find
│       heap_size_limit       │ '2048.00 MB' │
│       malloced_memory       │  '0.01 MB'   │
│    peak_malloced_memory     │  '2.93 MB'   │
│      does_zap_garbage       │  '0.00 MB'   │
│  number_of_native_contexts  │  '0.00 MB'   │
│ number_of_detached_contexts │  '0.00 MB'   │
└─────────────────────────────┴──────────────┘
memory info
┌─────────────────────────────┬──────────────┐
│           (index)           │    Values    │
├─────────────────────────────┼──────────────┤
│       total_heap_size       │ '521.66 MB'  │
│ total_heap_size_executable  │  '1.05 MB'   │
│     total_physical_size     │ '520.90 MB'  │
│    total_available_size     │ '1610.56 MB' │
│       used_heap_size        │ '436.35 MB'  │ // <--- stays around 435
│       heap_size_limit       │ '2048.00 MB' │
│       malloced_memory       │  '0.01 MB'   │
│    peak_malloced_memory     │  '2.93 MB'   │
│      does_zap_garbage       │  '0.00 MB'   │
│  number_of_native_contexts  │  '0.00 MB'   │
│ number_of_detached_contexts │  '0.00 MB'   │
└─────────────────────────────┴──────────────┘

V8 只有在需要释放空间时才会进行 GC。默认情况下,Node 被赋予 ~2GB 的内存,因此它不会受到来自 435mb 对象的太大压力。

您可以通过以node --expose-gc server.js方式运行服务器来强制使用 GC,这会在全局上公开gc函数。

const v8 = require('v8');
// for memory allocation heap debugging
module.exports.v8Print = (message) => {
global.gc(); // <-- Call this before reading heap info
const heapStats = v8.getHeapStatistics();
const heapStatsMB = heapStats;
for (const key in heapStatsMB) {
heapStatsMB[key] = `${(((heapStatsMB[key] / 1024 / 1024) * 100) / 100).toFixed(2)} MB`;
}
console.log('');
console.log(message);
console.table(heapStatsMB);
console.log('');
};
// ...
app.get('/orders', async (req, res, next) => {
v8Print('before find orders'); // ~50MB
let j = await Order.find({}); // Use `let` merely so we can set it to `undefined` later
v8Print('afters find orders'); // ~435MB
j = undefined; // Remove reference so it can be GC'd
v8Print('afters clean up'); // ~50MB
res.sendStatus(200);
});
app.get('/logMem', async (req, res, next) => {
v8Print('memory info'); // ~50MB
res.sendStatus(200);
});

最新更新