The idea of Buffer is to handle, process raw binary data which may appear when node.js and external data sources like network, filesystem are communicating.
For such purposes Node use system API calls (mmap()
in unix, VirtualAlloc()
in windows mainly used for allocating anonymous memory regions -> physical memory of the computer system, not V8)
Before Node.js 4.0: Buffers were allocated in the heap memory of the V8 engine, along with JavaScript objects.
They were subject to garbage collection and had the same memory management as other JavaScript objects.
Node.js 4.0 and later: Buffers larger than a certain threshold (~8 KB) are allocated
outside the V8 heap in a separate memory pool called "Buffer pool" or "Backing store." (related topic)
This memory is managed by Node.js directly using native system calls or APIs.
The advantage of allocating larger Buffers outside the V8 heap is to reduce pressure on the garbage collector and improve performance for handling binary data.
Smaller Buffers that are below the threshold are still allocated within the V8 heap and are subject to garbage collection.
Node.js 12.0 and later: The Buffer.poolSize property was introduced, allowing you to configure the size of the Buffer pool.
By default, it is set to 8192 bytes (8 KB). You can adjust this value based on your application's needs.
So summary to your questions:
- where: Node.js stores buffers outside of V8 heap if size of buffer hits the limit
which might be configured using Buffer.poolSize property
- why: to make processing of large binary data more easier
Hope that helps :)