Profiling und Erkennung von Memory-Leaks
Lernen Sie, CPU- und Speichernutzung in Node.js zu messen, Profile zu erfassen und Memory-Leaks aufzuspüren, bevor sie die Produktion zum Absturz bringen.
Profiling und Erkennung von Memory-Leaks ist eine kostenlose Node.js Backend Development Bootcamp-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Node.js Backend Development Bootcamp-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Node.js Backend Development Bootcamp-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Why Profile?
You cannot optimize what you cannot measure. Profiling reveals where your app spends time (CPU) and memory, so you fix the real bottleneck instead of guessing.
A common rule: measure, change one thing, measure again.
Measuring Memory Usage
Node exposes live memory stats via process.memoryUsage(). The key field is heapUsed — the JavaScript heap your code allocates.
const m = process.memoryUsage();
console.log('Heap used MB:', (m.heapUsed / 1048576).toFixed(1));What is a Memory Leak?
A memory leak happens when objects that are no longer needed are still referenced, so the garbage collector cannot free them. Over time heapUsed climbs and the process eventually crashes.
Common Leak Sources
Most Node.js leaks come from a handful of patterns:
- Growing global arrays or caches that never evict
- Forgotten event listeners
- Closures capturing large objects
- Timers that are never cleared
const cache = [];
app.get('/x', (req, res) => {
cache.push(req.body); // never cleared = leak
res.end();
});Timing Code Sections
For quick CPU timing, console.time and console.timeEnd measure how long a block takes.
console.time('work');
for (let i = 0; i < 1e6; i++) {}
console.timeEnd('work');High-Resolution Timing
For precise benchmarks use the Performance API, which gives sub-millisecond resolution unaffected by clock changes.
const { performance } = require('perf_hooks');
const start = performance.now();
doWork();
console.log('Took', performance.now() - start, 'ms');CPU Profiling with --prof
Run Node with the --prof flag to record a V8 CPU profile. It writes a log file you process into a readable report.
// node --prof app.js
// node --prof-process isolate-*.log > report.txtHeap Snapshots
A heap snapshot captures every object in memory at a moment. Take two snapshots over time and compare them to find what keeps growing.
Capture them via Chrome DevTools (connect with --inspect) or programmatically.
// node --inspect app.js
// then open chrome://inspect and take heap snapshotsThe Clinic.js Toolkit
The clinic tool suite visualizes performance problems. Clinic Doctor diagnoses issues, while Clinic Heap Profiler tracks allocations.
// npm install -g clinic
// clinic doctor -- node app.jsDetecting a Leak in Practice
Log heapUsed periodically under steady load. If it trends upward and never returns after garbage collection, you likely have a leak — then snapshot to find the culprit.
setInterval(() => {
const mb = process.memoryUsage().heapUsed / 1048576;
console.log('Heap MB:', mb.toFixed(1));
}, 5000);Fixing Leaks
Once found, common fixes are:
- Cap caches with an LRU strategy
- Remove listeners with
removeListener/off - Clear timers with
clearInterval - Avoid capturing large objects in long-lived closures
Quick Check
Test your profiling knowledge.
Recap
You learned to profile and find leaks:
- Measure memory with
process.memoryUsage() - Time code with
console.timeand the Performance API - Capture CPU profiles with
--profand heap snapshots with--inspect - Spot leaks via climbing
heapUsed; fix with bounded caches and cleaned-up listeners/timers
Measuring first turns optimization from guesswork into engineering.
Häufig gestellte Fragen
Ist die Lektion „Profiling und Erkennung von Memory-Leaks“ kostenlos?
Ja — der vollständige Text von „Profiling und Erkennung von Memory-Leaks“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Node.js Backend Development Bootcamp-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Node.js Backend Development Bootcamp-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Profiling und Erkennung von Memory-Leaks“?
Lernen Sie, CPU- und Speichernutzung in Node.js zu messen, Profile zu erfassen und Memory-Leaks aufzuspüren, bevor sie die Produktion zum Absturz bringen. Du übst Node.js Backend Development Bootcamp mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Node.js Backend Development Bootcamp zu starten?
Keine Vorkenntnisse erforderlich. Node.js Backend Development Bootcamp auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Profiling und Erkennung von Memory-Leaks“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Node.js Backend Development Bootcamp-Lektion Code schreiben und ausführen?
Ja. Jede Node.js Backend Development Bootcamp-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Caching-Strategien für Node.js
- Load-Balancing für Ihre Node.js-Apps
- Die Node.js Event Loop optimieren
- Profiling und Erkennung von Memory-Leaks