memory-leak-detection

Detect and fix memory leaks using heap snapshots, memory profiling, and leak detection tools. Use when investigating memory growth, OOM errors, or optimizing memory usage.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "memory-leak-detection" with this command: npx skills add aj-geddes/useful-ai-prompts/aj-geddes-useful-ai-prompts-memory-leak-detection

Memory Leak Detection

Table of Contents

Overview

Identify and fix memory leaks to prevent out-of-memory crashes and optimize application performance.

When to Use

  • Memory usage growing over time
  • Out of memory (OOM) errors
  • Performance degradation
  • Container restarts
  • High memory consumption

Quick Start

Minimal working example:

import v8 from "v8";
import fs from "fs";

class MemoryProfiler {
  takeSnapshot(filename: string): void {
    const snapshot = v8.writeHeapSnapshot(filename);
    console.log(`Heap snapshot saved to ${snapshot}`);
  }

  getMemoryUsage(): NodeJS.MemoryUsage {
    return process.memoryUsage();
  }

  formatMemory(bytes: number): string {
    return `${(bytes / 1024 / 1024).toFixed(2)} MB`;
  }

  printMemoryUsage(): void {
    const usage = this.getMemoryUsage();

    console.log("Memory Usage:");
    console.log(`  RSS: ${this.formatMemory(usage.rss)}`);
    console.log(`  Heap Total: ${this.formatMemory(usage.heapTotal)}`);
    console.log(`  Heap Used: ${this.formatMemory(usage.heapUsed)}`);
    console.log(`  External: ${this.formatMemory(usage.external)}`);
// ... (see reference guides for full implementation)

Reference Guides

Detailed implementations in the references/ directory:

GuideContents
Node.js Heap SnapshotsNode.js Heap Snapshots
Memory Leak Detection MiddlewareMemory Leak Detection Middleware
Common Memory Leak PatternsCommon Memory Leak Patterns
Python Memory ProfilingPython Memory Profiling
WeakMap/WeakRef for CacheWeakMap/WeakRef for Cache
Memory Monitoring in ProductionMemory Monitoring in Production

Best Practices

✅ DO

  • Remove event listeners when done
  • Clear timers and intervals
  • Use WeakMap/WeakRef for caches
  • Limit cache sizes
  • Monitor memory in production
  • Profile regularly
  • Clean up after tests

❌ DON'T

  • Create circular references
  • Hold references to large objects unnecessarily
  • Forget to clean up resources
  • Ignore memory growth
  • Skip WeakMap for object caches

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

nodejs-express-server

No summary provided by upstream source.

Repository SourceNeeds Review
General

markdown-documentation

No summary provided by upstream source.

Repository SourceNeeds Review
General

rest-api-design

No summary provided by upstream source.

Repository SourceNeeds Review
General

architecture-diagrams

No summary provided by upstream source.

Repository SourceNeeds Review