Memory Leak: Detect and Resolve

Identify memory leaks before they crash your server.

A memory leak occurs when an application doesn't release memory it no longer uses. Gradually, RAM fills up until it crashes. Here's how to detect and resolve this problem.

Symptoms

  • Memory consumption that continuously increases
  • Performance that degrades over time
  • Server crash after several hours/days
  • "Out of memory" errors or OOM Killer

Common Causes

  • Unreleased variables: Objects or arrays that accumulate without being destroyed.
  • Unclosed connections: DB connections or files opened but never closed.
  • Unbounded caches: An in-memory cache without size limits eventually consumes everything.

Diagnostic Steps

  1. Monitor RAM evolution with htop or monitoring
  2. Identify the process consuming the most
  3. Use profiling tools (Xdebug, Node --inspect)
  4. Check consumption patterns after restart

Automate with MoniTao

MoniTao detects memory leak symptoms:

  • Monitoring of degrading response times
  • Alerts on recurring 500 errors
  • Heartbeat to detect crashes

Best Practices

  • Regularly restart workers (pm2, supervisord)
  • Configure memory limits (php.ini memory_limit)
  • Use connections with pool and timeout
  • Implement RAM monitoring

FAQ

How to know if it's a memory leak?

If memory continuously increases without ever going down, it's a leak.

Can PHP have memory leaks?

Yes, especially with long loops or persistent workers (Swoole, ReactPHP).

Does restarting solve the problem?

Temporarily yes. But the leak will return. You need to fix the code.

How does MoniTao help detect this?

Progressively increasing response times are an indicator. 500 errors too.

Ready to Sleep Soundly?

Start free, no credit card required.