nodejs / help

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

Does Node.js Buffer.alloc not occupy memory space when it is not filled #4438

Open mengdu opened 3 days ago

mengdu commented 3 days ago

Node.js Version

v17.4.0

NPM Version

v8.3.1

Operating System

Darwin Kernel Version 21.6.0

Subsystem

buffer

Description


const format = function (bytes) {
  return (bytes / 1024 / 1024).toFixed(2) + ' MB';
};

const memoryUsage = function() {
  const memoryUsage = process.memoryUsage();
  return JSON.stringify({
    rss: format(memoryUsage.rss),
    heapTotal: format(memoryUsage.heapTotal),
    heapUsed: format(memoryUsage.heapUsed),
    external: format(memoryUsage.external),
    arrayBuffers: format(memoryUsage.arrayBuffers),
})
}

async function main() {
  const arr = []
  setInterval(() => {
    arr.push(Buffer.alloc(100 * 1024 * 1024))
    console.log(arr.length, memoryUsage())
  }, 100)
}

main()

The memoryUsage.external memory size has exceeded the limit, but the program is normal. In the monitor, the node process does not occupy a high amount of memory; What's going on here?

Minimal Reproduction

No response

Output

115 {"rss":"26.31 MB","heapTotal":"5.27 MB","heapUsed":"3.43 MB","external":"11500.41 MB","arrayBuffers":"11500.01 MB"} 116 {"rss":"26.31 MB","heapTotal":"5.27 MB","heapUsed":"3.43 MB","external":"11600.41 MB","arrayBuffers":"11600.01 MB"} 117 {"rss":"26.31 MB","heapTotal":"5.27 MB","heapUsed":"3.43 MB","external":"11700.41 MB","arrayBuffers":"11700.01 MB"} 118 {"rss":"26.31 MB","heapTotal":"5.27 MB","heapUsed":"3.43 MB","external":"11800.41 MB","arrayBuffers":"11800.01 MB"} 119 {"rss":"26.31 MB","heapTotal":"5.27 MB","heapUsed":"3.43 MB","external":"11900.41 MB","arrayBuffers":"11900.01 MB"} 120 {"rss":"26.31 MB","heapTotal":"5.27 MB","heapUsed":"3.43 MB","external":"12000.41 MB","arrayBuffers":"12000.01 MB"} 121 {"rss":"26.31 MB","heapTotal":"5.27 MB","heapUsed":"3.43 MB","external":"12100.41 MB","arrayBuffers":"12100.01 MB"}

Before You Submit

RedYetiDev commented 2 days ago

@nodejs/buffer