Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Tracing What Keeps a Match Alive

Tracing What Keeps a Match Alive

In this lesson, you trace what keeps a match alive in JavaScript. The match-log processor’s scores are right, but it still holds a whole day of match records. Your Python teammate’s fix: set each match to None when the handler’s done with it.

In Python, the handler builds match M1 as a small dataclass and appends it to history, the replay list. weakref.finalize() prints a line the moment CPython frees M1. Then the script sets match to None and clears the history:

Language: Python
import weakref
from dataclasses import dataclass

@dataclass
class Match:
    id: str
    points: int

match = Match("M1", 3)
weakref.finalize(match, print, "M1 freed")
history = []
history.append(match)
match = None
print("match is None")
history.clear()
print("history cleared")

Node’s REPL runs the JavaScript port line by line and echoes each result. A let holds the match, since the port reassigns it, and a const holds the history. Declarations echo undefined, since they have no value. .push() echoes the new length, 1, and match = null echoes null:

Language: Node.js
> let match = { id: "M1", points: 3 };
undefined
> const history = [];
undefined
> history.push(match);
1
> match = null;
null
> history
[ { id: 'M1', points: 3 } ]

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In this lesson, you’ll trace what keeps a match alive in JavaScript. The processor’s scores are right, but it still holds a whole day of match records. Your Python teammate’s fix: set each match to None when the handler’s done with it.

00:15 In Python, the handler builds match M1 as a small dataclass, and appends it to history, the replay list. weakref.finalize() prints a line the moment CPython frees M1.

00:28 Then the script sets match to None and clears the history. Node’s REPL runs the JavaScript port line by line and echoes each result.

00:40 A let holds the match, since the port reassigns it, and a const holds the history. Declarations echo undefined, since they have no value.

00:48 .push() echoes the new length, 1, and match = null echoes null. After match = null, is M1 gone, or still in the history?

01:01 Your turn: pause the video and predict.

01:09 It’s still there: the history holds M1.

01:15 Every variable, array slot or property that points at an object is a strong reference: it keeps that object alive. The engine starts from roots, the variables your running code can still reach, like history.

01:29 From that root, a reference goes to the array, and from its first slot, one goes to M1. match = null removed only the reference from match.

01:40 An object that a chain of strong references reaches from a root is reachable, and the collector never frees it. So M1 stays. Python agrees: CPython didn’t free M1 on match = None either, because the list still held it.

01:58 So the fix is to clear the real owner: the history. JavaScript arrays have no clear() method; setting .length to 0 empties the array in place.

02:09 The REPL echoes 0, and history is now empty. No root reaches M1 now, so it’s unreachable, and the garbage collector may reclaim it.

02:20 The difference is when: CPython printed M1 freed on the clear() line, because its reference count hit zero there. That’s CPython’s detail: Python’s language reference promises only that unreachable objects may be collected, later or never.

02:37 JavaScript promises no line either: V8 keeps no reference counts, and its collector reclaims unreachable objects whenever it runs.

02:47 Node’s process.memoryUsage() returns the whole process’s memory, in bytes. rss is the process’s memory resident in RAM, and heapUsed is JavaScript’s heap in use, live objects and uncollected garbage alike.

03:04 These are observations from one run; yours will differ. Read twice in a row, heapUsed changed, though none of your data did. One small match is far smaller than that drift.

03:17 Even for a match holding a million numbers, heapUsed was no lower right after the clear; memory comes back only when the collector runs. So a cleared owner proves your program cut a reference, not that memory was collected.

03:33 One more shape: suppose M1 gets a replay object that points back at M1. That’s a cycle. Watch out for the old advice to break every cycle by hand: it comes from reference counting, and sends you hunting in the wrong place.

03:48 Once no root reaches the pair, the whole cycle is unreachable, and the collector can reclaim both. CPython’s counts alone never free a cycle; its separate cyclic collector finds it later.

04:01 V8’s collector traces from the roots, so cycles need no special case.

04:08 So in JavaScript, trace from the roots and clear the owners that still reach your data. Setting a local to null cuts one reference and frees nothing by itself: that’s what you tell your Python teammate.

04:21 As in good Python, a JavaScript developer doesn’t null locals: const holds the match inside the handler, where it stops being a root once the handler returns, and the long-lived owner stays bounded.

04:34 Next up is the exercise Bound the Replay History. There, you’ll cap a replay history, keep the newest records in order, and release each record you evict.

Become a Member to join the conversation.