Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging PHP memory leak. Tracking the reference count?

I have a rather large PHP script that is leaking memory. I have been able to figure out what object is leaking, but I have not been able to find out where in the script it is leaking. Some part of my script is keeping a reference to the leaking object.

Is there some way that I can track (and preferably backtrace) where/when a PHP object has it's reference count increased or decreased?

like image 575
Sander Marechal Avatar asked Nov 21 '11 08:11

Sander Marechal


People also ask

How do I debug memory usage?

When analyzing possible memory leaks, you need access to the app's memory heap. Then you can analyze the memory contents. Looking at relationships between objects, you create theories on why memory isn't being freed. A common diagnostics data source is a memory dump on Windows or the equivalent core dump on Linux.

What is a PHP memory leak?

What is a memory leak in PHP? A memory leak in PHP is a condition that causes sections of code to continue using memory even though that memory is no longer needed.

How do I check my memory overflow?

To find a memory leak, you've got to look at the system's RAM usage. This can be accomplished in Windows by using the Resource Monitor. In Windows 11/10/8.1: Press Windows+R to open the Run dialog; enter "resmon" and click OK.


2 Answers

You can use profiling tools to find out what is leaking your memory i prefer http://xdebug.org/ and http://code.google.com/p/webgrind

http://derickrethans.nl/xdebug-and-tracing-memory-usage.html

like image 175
Aurimas Ličkus Avatar answered Sep 22 '22 08:09

Aurimas Ličkus


Try to use:

  • http://www.php-debug.com
  • http://php.net/manual/en/function.debug-backtrace.php
  • http://xdebug.org/docs/execution_trace

Also, I suggest to write the code in a easy-to-debug manner and separate the debugging procedures from the code.

like image 35
Narcis Radu Avatar answered Sep 23 '22 08:09

Narcis Radu