mcpField
M
memory-leak-detection

memory-leak-detection

github.com/aj-geddesDevelopment373583 installsUpdated 34w ago
Repository

About memory-leak-detection

A troubleshooting guide for developers to identify and fix memory leaks, including cases of growing memory use, OOM errors, and performance degradation. References cover Node.js and Python profiling.

Key features

  1. 01Use Node.js heap snapshots
  2. 02Identify common leak patterns
  3. 03Detect leaks with middleware
  4. 04Profile memory in Python
  5. 05Manage caches with WeakMap and WeakRef

Use cases

  1. 01Investigate steadily growing memory use
  2. 02Diagnose OOM errors
  3. 03Investigate performance degradation
  4. 04Monitor memory in production

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill memory-leak-detection

After installing, type/memory-leak-detectionin Claude Code to confirm it loaded.

Bundled files

References and scripts installed along with SKILL.md.

references/
common-memory-leak-patterns.md
memory-leak-detection-middleware.md
memory-monitoring-in-production.md
nodejs-heap-snapshots.md
python-memory-profiling.md
weakmapweakref-for-cache.md
scripts/
validate-config.sh
templates/
config-starter.yaml

Details

From the project README

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

Comments

Markdown supported