Where can possibly be a memory leak in php and how to investigate it? Where can possibly be a memory leak in php and how to investigate it? codeigniter codeigniter

Where can possibly be a memory leak in php and how to investigate it?


A very old question, but for those facing this problem (since it is still a problem for some of us using CodeIgniter)... per the developer: By default, CodeIgniter keeps an array containing your history of queries.

Look into setting save_queries to false in your database configuration.

I had the same problem with a work project and this dramatically reduced our memory usage.


If you need to find the exact cause of your memory leak, I will suggest you to use a memory profiler like XHProf.

This is the best way to debug the php scripts and what is the cause of memory leak.


Look out for:

1) Cron jobs

2) Queries using '*', and

3) Queries that use tables without INDEXES.

4) Specially, long, unoptimized queries. They are queries with lots of subselects, for exemple.

5) Run those queries with an EXPLAIN statement, and find out which tables should be optimized. Read here how to use explain: Using explain

You should install something like New Relic on your server/app, it has a free tier mode that can help you locate long queries and processes.

Now, I'm assuming you there isnt' any major processing that you are doing and didn't tell us about, for example, long lists of image processing.

That could kill a beast of a machine if unoptimized.

Remember that long loops can also be a memory leak. If thats your case, you could experiment switching from foreach loops for for loops, for example.