liveBook · Manning

Memory fragmentation

As explained in JavaScript in Depth

The condition in which usable memory is divided into separated free or occupied regions, reducing allocation or reclamation efficiency.

Memory fragmentation occurs when allocation and reclamation leave usable memory split into separated regions. In a garbage-collected heap, dead objects can leave holes between live objects; those gaps waste space, reduce cache efficiency, and make allocation more expensive. section 9.2.3 section 9.3.1

The collector and allocator determine how fragmentation is controlled:

  • V8 compacts sufficiently fragmented old-generation pages by moving live objects together and updating every pointer, but pauses JavaScript execution during the move. section 9.2.3
  • JSC uses fixed-size cells in 16KB blocks and does not copy surviving young objects, so live cells can remain scattered among empty cells. section 9.3.1
  • Non-moving collection simplifies native integration because C++ code can retain raw pointers, while moving collection requires pointer-updating handles. section 9.3.1

The partially empty block

A JSC block may contain live objects scattered among freed cells. Because the block is only partially empty, it cannot be reclaimed wholesale; the allocator must search free cells, while cache lines may carry dead space alongside live data. section 9.3.1

How JavaScript in Depth covers it

How deep it goes
Traces holes from object death through compaction or cell allocation, stopping short of allocation algorithms beyond the described mechanisms.
Examples
Uses V8 old-generation holes, compacted pages, and JSC blocks containing live objects among empty fixed-size cells.
What you can do after
Evaluate runtime memory behavior by relating scattered live objects to cache waste, allocation searches, compaction, and pause costs.
How it connects
Connects memory fragmentation with garbage collection, compaction, copying collection, allocators, cache performance, and raw-pointer management.

Also explained in

Compare how 3 books explain memory fragmentation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.