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

Published 5 months ago by asaketr64x

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?

Best Answer (As Selected By asaketr64x)
spekkionu

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.

jlrdw
jlrdw
5 months ago (252,700 XP)

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

spekkionu

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.

asaketr64x

thanks @spekkionu that was pretty helpful!

jlrdw
jlrdw
5 months ago (252,700 XP)

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

Cronix
Cronix
5 months ago (798,990 XP)

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.

avaneeshsingh

Thank you @spekkionu for this great piece of advice.

avaneeshsingh

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

Cronix
Cronix
5 months ago (798,990 XP)

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.

Cronix
Cronix
5 months ago (798,990 XP)

@avaneeshsingh Interestingly enough, it's mentioned in older docs, but not for more recent versions: https://laravel.com/docs/5.0/database#query-logging. It looks like it was removed from the docs for v5.1 and up.

avaneeshsingh

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
jlrdw
5 months ago (252,700 XP)

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
Cronix
5 months ago (798,990 XP)

Yes, but the end result is the same lol.

Please sign in or create an account to participate in this conversation.