8

I'm using Codeigniter framework (development version from github) for one of my projects. Project itself is not big just a few controllers and models and I have a memory leak. In 12 hours my rams constantly go up and i have to restart php5-fpm to clean them. Where should I start looking for memory leak? I mean is it loops or variables and what tools can I use for this to investigate?

hakre
  • 178,314
  • 47
  • 389
  • 754
d4mn
  • 101
  • 2
  • 1
    http://stackoverflow.com/questions/849549/diagnosing-memory-leaks-allowed-memory-size-of-bytes-exhausted – GoodSp33d Apr 10 '14 at 08:30
  • Read this SO question: http://stackoverflow.com/questions/18524984/solving-php-memory-leaks?rq=1 – Bud Damyanov Apr 10 '14 at 08:31
  • I used valgrind and found that memory leak does exists in my cms "in use at exit: 93,364 bytes in 1,447 blocks" The question is how to get more details about were is the memory leak using valgrind? because now i just see the address in php proccesor at 0x4C28BED: malloc (vg_replace_malloc.c:263) – d4mn Apr 10 '14 at 11:50
  • Have you tried kungphu's answer? – Chibueze Opata Jul 27 '16 at 10:46
  • why are you using a development copy of CI? and which version of CI are you using? 2 or 3? – b_dubb Jul 28 '16 at 20:40

3 Answers3

9

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.

Community
  • 1
  • 1
kungphu
  • 3,851
  • 2
  • 22
  • 31
1

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.

cedzz
  • 369
  • 2
  • 9
1

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.

André Tzermias
  • 842
  • 8
  • 13