Debug Node.js Memory Leaks
My Node.js application is experiencing memory leaks that become apparent after several hours of runtime, and I need help identifying and resolving them. Environment Details: - Node.js version: [version] - Framework: [Express/Fastify/etc] - Key dependencies: [list major packages] - Deployment: [Docker/PM2/bare metal] - Available memory: [RAM amount] Application Architecture: - Type: [API/websocket server/worker/etc] - Concurrent users/connections: [average number] - External services: [databases, caches, APIs] - Background jobs: [yes/no, what kind] Observed Symptoms: - Initial memory usage: [MB/GB] - Memory after [X] hours: [MB/GB] - Growth rate: [MB per hour] - Error messages: [OOM errors, etc] - Performance degradation: [response times, CPU usage] Current Monitoring: - Tools used: [PM2, New Relic, etc] - Heap snapshots taken: [yes/no] - Memory profiling attempted: [what was tried] Code Patterns Present: - Event listeners: [how they're managed] - Database connections: [pooling strategy] - File operations: [streaming vs loading] - Global variables usage: [yes/no] Please help me: 1. Identify common memory leak patterns in my setup 2. Provide debugging strategy with specific tools 3. Show code examples of proper resource cleanup 4. Recommend monitoring and alerting approach 5. Suggest preventive coding practices