A Node.js service is experiencing OutOfMemory errors, but monitoring shows the total free memory is still high. What is the most likely cause?
Node.js interview question for Advanced practice.
Answer
Severe heap fragmentation is preventing the allocation of a large, contiguous memory block.
Explanation
Heap fragmentation is the most likely cause. This occurs when free memory is broken into many small, non-contiguous blocks after numerous allocations and deallocations. Even if the total free memory is substantial, the application can fail to allocate a large object if no single free block is large enough. This leads to an OutOfMemory error despite technically having enough free space overall.