asaketr64x's avatar

What things can cause a memory leak in a laravel project?

I have heard about memory leaks but never faced one.I want to know what things can cause a memory leak in a LARAVEL PROJECT .Is it too much of sql queries or long process time or what?. Thanks for the suggestions. Also what practices should I follow to avoid it?

0 likes
13 replies
jlrdw's avatar

One thing to check is your PHP version, the exact build and check any issues.

1 like
spekkionu's avatar
Level 48

A memory leak is a specific type of problem where something is put in memory but never removed. This is much less a problem for a HTTP request as when the request is complete the process goes away so even if you have a memory leak it likely wont matter much. They are much more problematic for a long running process or a cli script.

https://en.wikipedia.org/wiki/Memory_leak

Php is pretty good at garbage collection so memory leaks aren't too common but they do happen from time to time.

Generally where you need to watch out for them is in loops. To avoid memory leaks in your scripts you really just need to close any open file streams, destroy image resources, or close any open socket connections when you no longer need them. Also if you have a variable that takes up a large amount of memory you might want to unset it yourself one you don't need it anymore rather than wait for the garbage collector to do it for you.

The other thing you might need to watch out for is cyclic references.

https://en.wikipedia.org/wiki/Circular_reference

Overall this isn't something you should be too worries about. I've probably only had one cause an issue a dozen times or so in the 12+ years I've been programming in php.

3 likes
jlrdw's avatar

I have seen where MySQL has had memory leaks in the past, again check issues of any package you desire.

Cronix's avatar

One thing laravel does is track all queries that are run in memory for each request cycle. For regular pages, this isn't a big deal, but if you are running a long process that is making thousands of queries, it could overrun the memory. Like I have a process that takes several hours pulling down real estate listings from various api's, and parsing the results and inserting the records into the db, which results in hundreds of thousands of queries. It would run fine for about 30 minutes or so until it overran the memory (512M). The only fix I've found for it is:

\DB::disableQueryLog();

and then it would run for hours and the entire process consumed no more than 6M, and it was a lot faster as well. So, I just put that before any long running process and it's been fine.

4 likes
AvaneeshSingh's avatar

@Cronix Is QueryLog enabled by default? I am asking because I had to enable it manually while working with Laravel 5.4

Cronix's avatar

This was actually on a 5.4 project, and it was enabled by default. Not sure if it has something to do with the environment (local/production/etc), but I was using local at the time.

1 like
AvaneeshSingh's avatar

Thanks for the link.

That's really interesting. Newer docs are supposed to be more explanatory and here we can see a good piece of text has been removed.

jlrdw's avatar

Remember however there is a difference in a memory leak which could be a bug versus eating up too much memory.

In your question you said memory leak.

Cronix's avatar

Yes, but the end result is the same lol.

mesiarm's avatar

DB::getConnection()->unsetEventDispatcher(); helped me

Please or to participate in this conversation.