AI summary by TwinMind

An unexpected memory leak in JS

Theo - t3․gg

13m 55s

Introduction to Garbage Collection

  • Garbage collection exists in many languages, not just Go
  • Languages like Rust don't have GC, require manual memory cleanup
  • GC collects memory left behind after code execution completes
  • Makes coding easier but has flaws, especially in JavaScript

Jake Surma and Jason's Discovery

  • Discovered unexpected GC behavior within functions
  • Example: ArrayBuffer with timeout function that returns cancel function
  • Expected: memory cleanup after timeout executes or gets cancelled
  • Reality: big array buffer leaked forever despite function no longer being callable

Memory Leak Mechanics

  • Reference counting chain: big array buffer → anonymous function → setTimeout → ID → cancel function
  • Global.cancelDemo still exists, references ID
  • Reference tracker prevents big array buffer deallocation
  • Leak occurs even when returned cancel function doesn't reference big array buffer

Working Examples (No Leaks)

  • Simple array buffer with immediate logging - gets cleaned up properly
  • setTimeout without return value - reference counter dies after execution
  • Array buffer not referenced in inner functions - no tracking needed

Problematic Pattern Analysis

  • Engine sees big array buffer referenced by inner functions
  • Associated with scope created when demo function called
  • After 1 second, function referencing buffer no longer callable
  • But scope remains because cancel function still callable
  • Big array buffer associated with scope, remains in memory

Solution and Testing

  • Setting Global.cancelDemo = null enables garbage collection
  • Wipes last available reference
  • Issue not specific to timers - affects any function scope bindings

Live Browser Testing Results

  • Initial memory usage: 101 megabytes
  • Setting global.innerFunc1 = null: still 100MB
  • Setting global.innerFunc2 = null: drops to 2.5MB
  • Confirms both references must be cleared

Scope Isolation Experiments

  • Moving function definition outside scope: memory cleans up properly
  • Any Global.this binding in same scope prevents cleanup
  • Function scope cannot be cleaned up until every Global.this binding cleared
  • Cross-browser issue, not specific to one engine

Key Finding

  • Creation of function calling Global.this at same scope level as Global.this assignment
  • Prevents reference clearing until Global.this reference cleared first
  • Affects any variables in shared scope, not just directly referenced ones
  • Memory leak persists until all Global.this bindings in scope dereferenced

TwinMind

Get summaries like this for your own meetings

TwinMind takes notes in the background during meetings, lectures, and calls, then writes the summary for you.

Free plan to get started

No bot joins your calls

Phone, desktop & Chrome apps

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome