當我運行await Order.find({})
回傳 22k 個檔案時,它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' │
└─────────────────────────────┴──────────────┘
uj5u.com熱心網友回復:
V8 只有在需要釋放空間時才會 GC。默認情況下,Node 會獲得約 2GB 的記憶體,因此它不會受到 435mb 物件的太大壓力。
你可以通過運行你的服務器來強制 GC,因為它在全域上node --expose-gc server.js
公開了一個函式。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);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/506439.html
標籤:javascript 节点.js mongodb 猫鼬 内存泄漏
上一篇:使用concat展平陣列時出錯
下一篇:React...constcastError=newCastError();...編輯操作將我退出(出現錯誤時我設定的條件)