Senior LevelNode JS
How does internal Buffer pooling (Buffer.poolSize = 8192) work and what causes severe memory fragmentation?
❓ Interview Prompt: Explain Node.js internal 8KB Slab Buffer (`Buffer.poolSize`), small buffer allocations (`Buffer.from()`), and memory retention bugs.
To optimize small allocations, Node.js pre-allocates an 8KB internal Slab Buffer ('Buffer.poolSize = 8192'):
Memory Retention Trap:
When you allocate small buffers (e.g. 10-byte slice), Node slices from this shared 8KB Slab. If a single 10-byte slice is retained in a global cache, the ENTIRE 8KB parent Slab is prevented from Garbage Collection!
Code Implementation Example
javascript
// Retaining a small slice keeps the parent 8KB Slab in RAM!
const smallSlice = Buffer.from('hello'); // Allocates from 8KB Slab
// FIX for persistent caching: Copy buffer memory to un-pool it!
const unpooledBuffer = Uint8Array.prototype.slice.call(smallSlice);💡Un-pooling small Buffers to prevent Slab memory fragmentation.
🎯 Interview Key Takeaway
Copy long-lived small Buffers to un-pool them from the shared 8KB Slab and prevent memory fragmentation.
#Buffer Pool#Slab Allocation#Memory Retention#Senior
Back to all Node JS Questions→