Q44: What are common causes of memory leaks in Express applications and how do you prevent them?

Difficulty:AdvancedTopic:Debugging

Common memory leak causes include:

  • Unbounded in-memory arrays acting as caches.
  • Event listeners added inside request middleware without being removed.
  • Unclosed database cursors or connection pools.
  • Storing session data in-memory (MemoryStore) in production environments.
Key Takeaways
  • Never use default MemoryStore for sessions in production; use Redis or database session stores.
  • Monitor process heap growth using heap dumps and tools like Chrome DevTools or clinic.js.