_CORE
AI & Agentic Systems Core Information Systems Cloud & Platform Engineering Data Platform & Integration Security & Compliance QA, Testing & Observability IoT, Automation & Robotics Mobile & Digital Banking & Finance Insurance Public Administration Defense & Security Healthcare Energy & Utilities Telco & Media Manufacturing Logistics & E-commerce Retail & Loyalty
References Technologies Blog Know-how Tools
About Collaboration Careers
CS EN
Let's talk

Memory leak hunting v Node.js

02. 03. 2025 1 min read intermediate

Node.js žere paměť? Systematický návod jak najít a opravit memory leaky.

Detekce

setInterval(() => console.log(Heap: ${Math.round(process.memoryUsage().heapUsed/1024/1024)} MB), 10000);

Chrome DevTools

node –inspect app.js

chrome://inspect → Memory → Heap snapshot

Příčiny

Event listenery

// ❌ emitter.on(‘data’, handler) v loopu
// ✅ emitter.once() nebo removeListener

Closures

Closure drží referenci na velký objekt, který by jinak GC sebral.

Globální cache

// ❌ const cache = {}; cache[key] = data; // roste neomezeně
// ✅ LRUCache s max size

Neuzavřené streamy

Vždy uzavírejte DB connections, file streams, HTTP connections.

LRU Cache

import { LRUCache } from ‘lru-cache’;
const cache = new LRUCache({ max: 500 });

Postup

Heap snapshot → porovnat 3 snapshoty → najít rostoucí objekty → opravit.

node.jsmemorydebugging
Share:

CORE SYSTEMS tým

Stavíme core systémy a AI agenty, které drží provoz. 15 let zkušeností s enterprise IT.