Skip to main content
coding advanced

Fix Memory Leak in Application

Advanced AI prompt for debugging memory leaks. Get step-by-step analysis, tools, and solutions to identify and resolve memory issues.

Works with: chatgptclaudegemini

Prompt Template

I need help debugging a memory leak in my [PROGRAMMING_LANGUAGE] application. Here are the details: **Application Context:** - Programming Language: [PROGRAMMING_LANGUAGE] - Framework/Runtime: [FRAMEWORK] - Application Type: [APPLICATION_TYPE] - Environment: [ENVIRONMENT] **Memory Leak Symptoms:** [SYMPTOMS] **Code Snippet (if available):** ``` [CODE_SNIPPET] ``` **Current Memory Usage Patterns:** [MEMORY_PATTERNS] Please provide a comprehensive debugging approach that includes: 1. **Root Cause Analysis**: Analyze the provided information and identify potential causes of the memory leak 2. **Debugging Strategy**: Recommend specific tools and techniques for [PROGRAMMING_LANGUAGE] to profile and monitor memory usage 3. **Code Review**: If code is provided, review it for common memory leak patterns (unclosed resources, event listener leaks, circular references, etc.) 4. **Step-by-Step Investigation Plan**: Provide a methodical approach to isolate and identify the leak source 5. **Immediate Fixes**: Suggest quick wins or temporary mitigations 6. **Long-term Solutions**: Recommend architectural improvements and best practices to prevent future memory leaks 7. **Monitoring & Prevention**: Advise on ongoing monitoring strategies and code review practices Focus on practical, actionable solutions with specific code examples where applicable. Prioritize solutions by impact and implementation difficulty.

Variables to Customize

[PROGRAMMING_LANGUAGE]

The programming language of your application

Example: JavaScript (Node.js)

[FRAMEWORK]

Framework or runtime environment being used

Example: Express.js with MongoDB

[APPLICATION_TYPE]

Type of application experiencing the memory leak

Example: REST API server handling file uploads

[ENVIRONMENT]

Environment where the leak occurs

Example: Production Linux server with 8GB RAM

[SYMPTOMS]

Observable symptoms of the memory leak

Example: Memory usage grows from 200MB to 2GB over 6 hours, server crashes with out-of-memory errors

[CODE_SNIPPET]

Relevant code that might be causing the leak

Example: app.post('/upload', upload.single('file'), async (req, res) => { /* file processing code */ })

[MEMORY_PATTERNS]

Any patterns noticed in memory usage

Example: Memory spikes during file upload operations, garbage collection not freeing memory effectively

Example Output

## Root Cause Analysis Based on your Node.js file upload API, the memory leak likely stems from: 1. **Stream handling**: Uploaded files may not be properly closed/destroyed 2. **Buffer accumulation**: Large files being loaded entirely into memory 3. **Event listener leaks**: Uncleaned listeners on upload streams ## Debugging Strategy **Tools to use:** - `node --inspect` with Chrome DevTools heap profiler - `clinic.js` for production memory analysis - `heapdump` module for memory snapshots **Investigation steps:** 1. Enable heap profiling: `node --max-old-space-size=4096 --inspect app.js` 2. Take heap snapshots before/after upload operations 3. Monitor with: `process.memoryUsage()` logging ## Immediate Fixes ```javascript // Ensure stream cleanup app.post('/upload', upload.single('file'), async (req, res) => { try { // Process file await processFile(req.file); } finally { // Explicitly clean up if (req.file && req.file.stream) { req.file.stream.destroy(); } } }); ``` ## Long-term Solutions - Implement streaming file processing instead of loading entire files - Add request timeout and size limits - Use worker processes for file processing - Implement circuit breakers for memory-intensive operations

Pro Tips for Best Results

  • Always provide specific error messages, memory usage numbers, and timing information for more accurate diagnosis
  • Include relevant code snippets, especially around resource allocation and cleanup operations
  • Mention your monitoring setup and any profiling tools already tried to avoid duplicate suggestions
  • Specify the scale of your application (concurrent users, file sizes, etc.) to get appropriately scaled solutions
  • Follow up with specific questions about recommended tools or implementation details for clarification

Tags

Want 500+ Expert Prompts?

Get the Premium Prompt Pack — organized, tested, and ready to use.

Get it for $29

Related Prompts You Might Like