简体   繁体   English

Mongo调用导致节点js memory泄漏

[英]Mongo call leading to node js memory leak

When I run await Order.find({}) , which returns 22k documents, the used_heap_size goes up to around 430mb from around 50mb.当我运行await Order.find({})返回 22k 文档时, used_heap_size从大约 50mb 增加到大约 430mb。

It never seems to go back down. go 似乎永远不会退缩。

Shouldn't the used_heap_size go back down pretty soon? used_heap_size go 不应该很快回落吗? I'm not sure if this is defined as a memory leak, but it seems like it.我不确定这是否被定义为 memory 泄漏,但似乎是这样。 What causes this?这是什么原因造成的? Is this normal?这是正常的吗?

  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);
  });

Log function记录 function

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 will only GC if it needs to free up space. V8 只有在需要释放空间时才会 GC。 By default Node is given ~2GB of memory so it's not under much pressure from your 435mb of objects.默认情况下,Node 会获得约 2GB 的 memory,因此它不会受到 435mb 对象的太大压力。

You can force a GC by running your server as node --expose-gc server.js which exposes a gc function on the global.您可以通过将服务器作为node --expose-gc server.js运行来强制 GC,这会在全局上公开gc function。

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM