nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.45k stars 278 forks source link

Non heap memory is getting larger and larger #4235

Closed MarTuPoke closed 11 months ago

MarTuPoke commented 11 months ago

Details

My nodejs memory is getting larger and larger over time Both PM2 and Linux systemd cannot be avoided

So I first used heapdump to record the heap space size, but after my investigation, my heap space size has always been around 100MB, but my nodejs memory (rrs) will become larger and even reach 1GB+

Can you take a look at the heapsnapshot I provided

heapdump_2023_08_29_20_00_00__156.heapsnapshot.zip

After comparing some heapsnapshots, no issues were found, so I checked the memory situation through the OS of Nodejs. The code is as follows

const os = require('os');
setInterval(() => {
    console.log(`总内存${os.totalmem() / 1024 / 1024}MB,剩余内存${os.freemem() / 1024 / 1024}MB`);
    console.log('当前进程的内存使用情况:')
    // 获取当前程序的内存使用情况
    const used = process.memoryUsage();
    for (let key in used) {
        console.log(`${key} ${Math.round(used[key] / 1024 / 1024 * 100) / 100} MB`);
    }
    // rrs 代表常驻内存
    console.log(`常驻内存 ${Math.round((used.rss - used.heapTotal) / 1024 / 1024 * 100) / 100} MB`);
    // heapTotal 代表已申请到的堆内存
    console.log(`已申请到的堆内存 ${Math.round(used.heapTotal / 1024 / 1024 * 100) / 100} MB`);
    // heapUsed 代表已使用的堆内存
    console.log(`已使用的堆内存 ${Math.round(used.heapUsed / 1024 / 1024 * 100) / 100} MB`);
    // external 代表 V8 引擎内部的 C++ 对象占用的内存
    console.log(`V8 引擎内部的 C++ 对象占用的内存 ${Math.round(used.external / 1024 / 1024 * 100) / 100} MB`);
    // arrayBuffers 代表分配给 Node.js 的内存
    console.log(`分配给 Node.js 的内存 ${Math.round(used.arrayBuffers / 1024 / 1024 * 100) / 100} MB`);
    console.log('----------------------------------------')
    // 打印非堆内存
    console.log(`非堆内存 ${Math.round((used.rss - used.heapTotal) / 1024 / 1024 * 100) / 100} MB`);
}, 10000)

2023-08-30T01:51:04.983(Beijing Time)

image

As time goes on, the non heap memory of my program will become larger and larger. I don't know how to troubleshoot it anymore. I need help with everything. Thank you!

I will add screenshots of the memory size for the following time in the future

Node.js version

Not applicable.{ node: '16.17.1', v8: '9.4.146.26-node.22', uv: '1.43.0', zlib: '1.2.11', brotli: '1.0.9', ares: '1.18.1', modules: '93', nghttp2: '1.47.0', napi: '8', llhttp: '6.0.9', openssl: '1.1.1q+quic', cldr: '41.0', icu: '71.1', tz: '2022a', unicode: '14.0', ngtcp2: '0.1.0-DEV', nghttp3: '0.1.0-DEV' }

Example code

No response

Operating system

Ubuntu 18.04.4 LTS (GNU/Linux 4.15.0-180-generic x86_64)

Scope

Memory leak

Module and version

"dependencies": { "@alicloud/sms-sdk": "^1.1.6", "@hapi/joi": "15.1.1", "@koa/router": "^12.0.0", "async-lock": "^1.3.2", "axios": "^0.27.2", "cron": "^2.1.0", "croner": "^7.0.1", "cross-env": "^7.0.3", "crypto-js": "^4.1.1", "dayjs": "^1.11.5", "easy-monitor": "^2.2.8", "eventasync": "^1.0.0", "exceljs": "^4.3.0", "heapdump": "^0.3.15", "ioredis": "^5.2.2", "koa": "^2.13.4", "koa-body": "^6.0.1", "koa-ratelimit": "^5.0.1", "koa-response-time": "^2.1.0", "koa2-validation": "^1.1.0", "log4js": "^6.6.1", "mqtt": "^5.0.3", "ms": "^2.1.3", "mysql2": "^3.6.0", "nanoid": "3.3.4", "node-cmd": "^5.0.0", "node-crc16": "^2.0.7", "nodemailer": "^6.7.8", "qiniu": "^7.8.0", "socket.io": "^4.5.2", "superagent": "^8.0.9", "vm2": "^3.9.11", "wechatpay-node-v3": "^2.1.0", "xml2js": "^0.4.23", "yaml": "^2.1.1" }, "devDependencies": { "@types/koa-router": "^7.4.4", "@types/xml2js": "^0.4.11", "nodemon": "^2.0.19" }

MarTuPoke commented 11 months ago

2023-08-30T08:54:29.208(Beijing Time)

image
MarTuPoke commented 11 months ago

2023-08-30T15:33:29.831

image
MarTuPoke commented 11 months ago

2023-08-30T18:38:20.353

heapUsed is normal but non heap memory Getting bigger and bigger

image
MarTuPoke commented 11 months ago

The problem has been investigated and is not related to node

https://github.com/imnemo/crc16/issues/19