Leaking memory in PHP

Asked

Viewed 145 times

4

Leaking memory or Leaky memory occurs when a computer program does bad memory allocation management such that memory that is no longer needed is not released.

I’d like to understand how the Leaking memory can occur in PHP?

How I could "debug" to fix memory leaks?

The issue is related to the use of the ORM Doctrine that is implying a high consumption/manipulation of data in memory. Transforming some processes into slow and heavy tasks and in some cases lacking memory to complete the request, causing the same cutting or generalized slowness by consuming too much machine resources.

In case anyone has any experience on memory-Leak in the use of Doctrine will also help.

  • What is already written in PHP you will not solve the memory-Leak problem, as explained already in responses, you can debug memory-Leak using a program called Valgrind, about Doctrine the problem of having slow processes and tasks should be in the queries you are doing, try to optimize the query using Native query when necessary, review the use of caches and also your relationships. The problem most of the time is not in the language, but in the misuse of it by the programmer.

2 answers

5


PHP essentially does not have memory leakage because it works with language-managed memory through a garbage collector based on reference count with additional collection of cycles, then the leak can only occur if you have a bug on it or the cycle collector is off. See zend.enable-gc, gc_enable() and gc_collect_cycles().

It is not simple to create cycles. If you are doing this you have a great chance to be with a design too confused.

Moreover, under normal conditions of use PHP is not even to have memory retained for exaggerated time since it scripts run for a very short time, so in practice if you didn’t even have a garbage collector it would make little difference since shortly afterwards the memory will be fully released.

If the Doctrine guy does something that’s causing problems report it to the creators or use something that doesn’t have such a serious problem.

If you are retaining too much memory you have to analyze the code, there is a magic formula that says look in such place and you will see the leak. Any potential allocation may be retaining memory. But retention is not leakage.

You may be withholding information for too long because you have left some resource that uses unmanaged memory open for too long. Every time you use an external resource such as a file, database, network, or other type of service you need to close it. But even this is not to cause problem because at the time the variable that keeps this resource open comes out of scope it is automatically released either by reference count or by the cycle collector. And if all this fails it will be closed at the end of script.

It may be that you are keeping some variable longer than you should.

Maybe he’s just carrying too much in his memory. You have to scan the code and go around where it does large loads of memory and see if it can load less or load little by little.

Read more about GC.

0

It is very common this happens when a query is made in the database and this is not closed, keeping it open consumes memory and network traffic.

A solution in mysql database is to use:

mysqli_close($nome_da conexão);

It can also be avoided by fixing programming errors and simplifying code. Avoiding creating loops within loops.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.