↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Avoiding Memory Leaks in NodeJS: Best Practices for Performance

The post explains that memory leaks in NodeJS can cause slowdowns, high latency, crashes, and inefficient resource use. It frames the topic through V8 memory management, then shows how global variables, multiple references, closures, timers, and event handlers can keep heap objects alive longer than needed.

It recommends reducing global scope use, preferring local scope, nullifying values when they are no longer needed, using stack memory effectively, copying objects when useful, avoiding unnecessary mutations, and cleaning up timers and listeners with clearTimeout, clearInterval, and proper removal.

Reading notes
#

  • Memory leaks are common across languages and can affect application performance even when memory is managed automatically.
  • In NodeJS, memory understanding starts with V8, where stack stores static data and heap stores objects and dynamic data.
  • Garbage collection reclaims orphan heap objects that are no longer referenced directly or indirectly.
  • A memory leak is described as an orphan heap block that is no longer used but has not been reclaimed.
  • Global variables are never garbage collected during the application lifetime, so they can hold large object graphs in memory.
  • Accidental globals can happen through undeclared assignment or assigning to this; strict mode helps prevent this in many cases.
  • Arrow functions can also create accidental globals, and ESLint no-invalid-this is suggested as a safeguard.
  • The global scope should be used sparingly, and large objects kept there should be cleared when no longer needed.
  • Stack variables are preferred when possible because stack access is faster and can help avoid accidental heap retention.
  • Destructuring only the needed fields from objects or arrays can reduce references kept alive in closures, timers, and handlers.
  • Heap use should be kept efficient by copying objects when appropriate, avoiding mutations, avoiding duplicate references, and keeping variables short-lived.
  • Closures can leak memory when they retain references to large objects, as shown in the example with theThing and replaceThing.
  • The leak in that closure example can be fixed by nullifying originalThing at the end of the function.
  • Timers should be cleared when finished, and event listeners or observers should be removed once their job is done.
  • The post ends by noting that Node-Memwatch and Node-Inspector can help debug memory issues.