Skip to content

QuizNot attempted

Closure memory leak

Which pattern most commonly leaks memory in a long-lived SPA?
1–4 to choose, Enter to submit
Show answer & explanation

Correct answer:

  • A listener/interval added on mount that closes over large data and is never removed on unmount
  • A closure keeps alive everything it references
  • if a long-lived object (window, document, a store, an interval) still holds the listener after unmount, the component's data and detached DOM can never be collected.
  • Clean up in the effect's return or with an AbortController signal.
  • Not const vs let: declaration keyword has no effect on reachability.
  • Not nested functions: they are collected normally once nothing references them.
  • Not returning arrows from hooks: fine unless something long-lived keeps holding them.