Linked Lists in Memory — Heap & Pointers Visualized
Understand how linked lists live in memory — heap nodes, stack head reference, pointer chasing, garbage collection, and interactive memory layout visualizer.
Introduction
Linked lists in memory are not one contiguous block like arrays. Each
node is a separate heap object; head on the stack (or in a parent struct) holds the first address.
Understanding this layout explains cache misses, pointer bugs, and GC behavior.
Quick index
| # | Section |
|---|---|
| 1 | Stack vs heap |
| 2 | Interactive visualizer |
| 3 | Full implementation |
| 4 | Pointer chasing |
| 5 | GC & memory leaks |
| 6 | Real-life examples |
1. Stack vs heap
Stack Heap (scattered)
───── ────────────────
head ───────────────→ 0x1A2 { 10, →0x3F8 }
0x3F8 { 20, →0x7C0 }
0x7C0 { 30, →null }| Location | Holds |
|---|---|
| Stack | head reference (8 bytes on 64-bit) |
| Heap | Node objects { value, next } |
| next | Memory address of another node |
2. Interactive visualizer
Linked Lists in Memory
Heap allocation, pointer references, and garbage collection — how nodes live outside contiguous storage.
Heap nodes
Nodes scattered in heap — not contiguous like arrays
Each node is a separate heap object — allocated independently, not adjacent.
Memory layout
// Stack: head → ref to 0x1A2
// Heap: 0x1A2 { val:10, next→0x3F8 }
// Heap: 0x3F8 { val:20, next→0x7C0 }head lives on the stack (or in a struct); nodes live on the heap.
3. Full implementation
function createNode(value) {
return { value, next: null, address: `0x${Math.random().toString(16).slice(2, 5)}` }
}
function buildList(values) {
if (!values.length) return null
const head = createNode(values[0])
let cur = head
for (let i = 1; i < values.length; i++) {
cur.next = createNode(values[i])
cur = cur.next
}
return head
}
function advanceHead(head) {
const old = head
head = head?.next ?? null
return { head, orphaned: old }
}
function traverse(head) {
const out = []
let cur = head
while (cur) {
out.push(cur.value)
cur = cur.next
}
return out
}Function calls with output
// ── Demo calls — output shown on the right ──
let head = buildList([10, 20, 30])
traverse(head) // → [10, 20, 30]
head = advanceHead(head).head // → head points to 20
traverse(head) // → [20, 30]
// node {10} orphaned → GC eligible4. Pointer chasing
Each current = current.next is a memory dereference — potentially a cache miss if nodes are far apart. Arrays iterate with index arithmetic on contiguous memory — much more CPU-cache friendly.
5. GC & memory leaks
// Orphan old head — GC reclaims in JS when unreachable
head = head.next
// LEAK in C: lose last pointer without free()
// head = head.next; // old node leaked if not freed6. Real-life examples
| System | Memory layout |
|---|---|
| JavaScript objects | Object properties stored as heap nodes — GC tracks references |
| DOM tree siblings | element.nextSibling / previousSibling — doubly linked in browser |
| File systems (FAT) | File allocation table chains disk blocks — linked by block index |
| Redis linked lists | Each list entry is a heap node with prev/next pointers |
| Memory leak in SPAs | Event listener holds node ref → GC can't reclaim detached DOM |
GC and detached nodes
Nodes live on the heap — breaking references lets the garbage collector reclaim memory.
head → node(1) → node(2) → null. Both reachable from head.
Real-world implementation
function createChain() {
const nodeA = { value: 1, next: null }
const nodeB = { value: 2, next: null }
nodeA.next = nodeB
return { head: nodeA, detached: nodeB }
}
function releaseHead(state) {
// only nodeB is reachable via state.detached
state.head = state.head.next
return state
}Full working code for the scenario above.
Function calls with output
| // ── Function calls with output ── | |
| let state = createChain() | → head → 1 → 2 → null |
Side output shows return value after each call — step through to trace execution.
Summary
Nodes live on the heap, linked by references. The head variable is your only entry unless you also keep tail.
Next reads: Linked List Types · Linked List Operations
Subscribe to my newsletter
Stay up to date and get notified when I share new contents.
No spam ever, unsubscribe anytime