help icon indicating copy to clipboard operation
help copied to clipboard

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

Open mengdu opened this issue 1 year ago • 1 comments

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

  • [X] I have looked for issues that already exist before submitting this
  • [X] My issue follows the guidelines in the README file, and follows the 'How to ask a good question' guide at https://stackoverflow.com/help/how-to-ask

mengdu avatar Jul 04 '24 14:07 mengdu

@nodejs/buffer

avivkeller avatar Jul 05 '24 00:07 avivkeller

🔂 @nodejs/buffer PTAL

avivkeller avatar Jul 20 '24 01:07 avivkeller

Okey

mengdu avatar Jul 20 '24 01:07 mengdu