Claude Code performance

Memory Leak in Long-Running Background Job

A background job that runs periodically starts consuming more memory with each execution. After hours of running, the application runs out of memory and crashes. The job itself is correct but something isn't being cleaned up properly.

Memory usage climbs steadily despite the job appearing to complete normally.

Error Messages You Might See

java.lang.OutOfMemoryError: Java heap space Memory usage climbs to 100% after running job repeatedly Process killed due to memory limit exceeded
java.lang.OutOfMemoryError: Java heap spaceMemory usage climbs to 100% after running job repeatedlyProcess killed due to memory limit exceeded

Common Causes

  1. Large collection (List, Set) accumulating items without clearing
  2. Event listeners registered but never unregistered, accumulating with each run
  3. Database connections or file handles not closed in finally block
  4. Strong references held to completed objects preventing garbage collection
  5. Timer or scheduled task not cancelled, creating new instances on top of old ones

How to Fix It

Use memory profiler to find memory growth (Java: JProfiler, YourKit). Look for accumulating collections. Ensure resources are closed with try-finally or try-with-resources. Unregister event listeners when done. Clear collections: list.clear(). Check for circular references. Use weak references if needed. Monitor memory: jmap -heap PID

Real developers can help you.

Dor Yaloz Dor Yaloz SW engineer with 6+ years of experience, I worked with React/Node/Python did projects with React+Capacitor.js for ios Supabase expert BurnHavoc BurnHavoc Been around fixing other peoples code for 20 years. Pratik Pratik SWE with 15+ years of experience building and maintaining web apps and extensive BE infrastructure Daniel Vázquez Daniel Vázquez Software Engineer with over 10 years of experience on Startups, Government, big tech industry & consulting. Matthew Butler Matthew Butler Systems Development Engineer @ Amazon Web Services Victor Denisov Victor Denisov Developer Costea Adrian Costea Adrian Embedded Engineer specilizing in perception systems. Latest project was a adas camera calibration system. Richard McSorley Richard McSorley Full-Stack Software Engineer with 8+ years building high-performance applications for enterprise clients. Shipped production systems at Walmart (4,000+ stores), Cigna (20M+ users), and Arkansas Blue Cross. 5 patents in retail/supply chain tech. Currently focused on AI integrations, automation tools, and TypeScript-first architectures. Vlad Temian Vlad Temian 15+ years shipping production infrastructure for startups. Former CTO at qed.builders (acquired by The Sandbox). Cursor ambassador and agentic tooling builder. I've scaled systems, automated deployments, and built observability tools for AI coding workflows. I specialize in taking vibe-coded apps from broken prototype to production-ready: fixing Supabase auth/RLS, Stripe integrations, deployment pipelines, and cleaning up AI-generated spaghetti. I build tools in this space (agentprobe, claudebin, micode) and understand both sides: how AI generates code and why it breaks. https://blog.vtemian.com/ Simon A. Simon A. I'm a backend developer building APIs, emulators, and interactive game systems. Professionally, I've developed Java/Spring reporting solutions, managed relational and NoSQL databases, and implemented CI/CD workflows.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help

Frequently Asked Questions

How to find memory leaks?

Use profiler (JProfiler, YourKit, Chrome DevTools). Take heap dumps before/after memory growth. Compare objects in memory.

How to fix common memory leaks?

Use try-finally for resource cleanup. Unregister listeners: obj.removeEventListener(). Clear collections before reuse: list.clear(). Use weak references for caches.

What's a proper resource cleanup pattern?

try-with-resources (Java 7+): try (Resource r = new Resource()) { use r } (auto-closes). Or try-finally with cleanup in finally block.

Related Claude Code Issues

Can't fix it yourself?
Real developers can help.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help