Leaking memory in PHP

2
Leaking memory leaky occurs when a computer program mismanages memory allocation in such a way that memory that is no longer needed is not released.

I'd like to understand how memory leakyng can occur in PHP?

How could I "debug" to fix memory leaks?

The issue is regarding the use of ORM Doctrine that is implying high consumption / manipulation of data in memory. Turning some processes into slow and heavy tasks, and in some cases, memory lacks to complete the request causing the process to be cut or generalized slowness due to too much machine resource consumption.

If anyone has any experience of memory-leak in using Doctrine, it will also help.

    
asked by anonymous 16.08.2017 / 13:52

2 answers

2

PHP essentially has no memory leak because it works with memory managed by the language 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 sink is turned off. See zend.enable-gc , gc_enable() and gc_collect_cycles() .

Creating cycles is not easy. If you are doing this you have a great chance of being too confused.

Moreover, under normal PHP usage conditions it is not even to have memory retained for exaggerated time since scripts run for very little time, so in practice if you did not even have a garbage collector you would do little difference since soon after the memory will be released integrally.

If Doctrine's does something that is causing problems, report it to creators or use something that does not have such a serious problem.

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

You may be holding information too long because you have left some resource that uses unmanaged memory opened 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 a problem because at the moment the variable holding this open resource goes out of scope it is released automatically either by the reference count or by the cycle sink. And if all this fails it will be closed at the end of script .

Maybe you're just carrying too much stuff in your memory. You have to analyze the code and go around where it makes large loads of memory and see if you can load less or load slowly.

Read more about GC .

    
16.08.2017 / 14:02
0

This is very common when a query is done in the database and it is not closed, keeping it open consumes memory and traffic on the network.

One solution in the mysql database is to use:

mysqli_close($nome_da conexão);

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

    
16.08.2017 / 14:00