Page 1 of 2

How to look for PHP memory leaks?

Posted: Sun Jun 11, 2006 12:07 pm
by daou
Does anyone have any good hints/references/programs to look for PHP memory leaks?
I am using PHP and Apache builds:

Apache/2.0.54 (Win32) PHP/5.0.4-dev

And yes, I will update those soon so there's no need to offer an update solution.

Posted: Sun Jun 11, 2006 4:14 pm
by feyd
what's your definition of memory leaks?

Posted: Sun Jun 11, 2006 4:29 pm
by alex.barylski
feyd wrote:what's your definition of memory leaks?
Thats what I was wondering...

Looking for memory leaks in PHP core or PHP code???

There is little he/she can do about leaks except upgrade anyways...

Perhaps he/she meant memory usage??? In which case download a resource monitor application which logs memory/system resource usage and look at the logs

Posted: Sun Jun 11, 2006 7:06 pm
by John Cartwright
If your definition of a memory leak is high memory usage, I think you may be looking for XDebug.

Posted: Sun Jun 11, 2006 11:16 pm
by daou
what's your definition of memory leaks?
Good point. Should have made that clearer.
I think you may be looking for XDebug.
Thanks, I'll have a look at it.
Perhaps he/she meant memory usage??? In which case download a resource monitor application which logs memory/system resource usage and look at the logs
This is partly what I was aiming at.

Also things such as unclosed db connections and anything in the code which might have long term performance effects on the server during high usage. Anything short of debugging the core.
Or does PHP take care of all memory handling? i.e. Should I twiddle my thumbs instead of worrying about memory? I guess the obsession with trying to find memory leaks in code comes from C experience.

Posted: Sun Jun 11, 2006 11:32 pm
by Christopher
daou wrote:Also things such as unclosed db connections and anything in the code which might have long term performance effects on the server during high usage. Anything short of debugging the core.
Or does PHP take care of all memory handling? i.e. Should I twiddle my thumbs instead of worrying about memory? I guess the obsession with trying to find memory leaks in code comes from C experience.
PHP frees everything at the end of every script. Most PHP programmers only free very large data that is being allocated repeatedly and let PHP free everything else. What's worse is that freeing memory in PHP may not actually free the memory. It is up to the PHP memory manager to decide whether it makes sense to free a the memory at the time unset() is called or to wait until the end of the script. This is all very alien to programer who come from languages where memory allocation is very important.

It is interesting that in PHP memory allocation is seen as a performance issue much more than a resource issue. Allocating memory takes time (e.g. very large classes) and that time is critical in PHP scripts which usually have a total execution time of a fraction of a second.

Posted: Mon Jun 12, 2006 1:13 am
by daou
How does PHP handle setting an object or array to NULL?
Is it equivalent to calling unset() and setting to NULL?

Posted: Mon Jun 12, 2006 10:46 am
by John Cartwright
unset removes the variable from memory, setting it to null keeps it in memory

Posted: Mon Jun 12, 2006 12:52 pm
by Chris Corbyn
Jcart wrote:unset removes the variable from memory, setting it to null keeps it in memory
Interestingly you can unset cast in PHP.

Code: Select all

(unset) $foo; //Casts as null

Posted: Mon Jun 12, 2006 1:30 pm
by Christopher
Jcart wrote:unset removes the variable from memory, setting it to null keeps it in memory
I'm not sure this is exactly true. The actual effects on memory of these operations is not that clear. For example, it has been noted that calling unset() does not change the amount of memory allocated at all, but setting vars to NULL actually reduces the amount of memory allocated. Also, unset() has specific behaviors with vars inside of functions and classes, as well as with references.

As I noted above, PHP does what it thinks is best for memory managment. Freeing variable in a larger allocation block is a waste of time in most cases so PHP simply does nothing for better performance.

Posted: Mon Jun 12, 2006 4:16 pm
by Maugrim_The_Reaper
There are also cases where unset() sucks execution time.

I think unset() is more a nudge/hint/wink function to PHP than a concrete "does what it says in the manual" option. I have a preference for using NULL on small units of data if pushed, unset I play around with on large memory assignments in conjunction with a profiler like XDebug.

Posted: Mon Jun 12, 2006 6:15 pm
by John Cartwright
arborint wrote:
Jcart wrote:unset removes the variable from memory, setting it to null keeps it in memory
I'm not sure this is exactly true. The actual effects on memory of these operations is not that clear. For example, it has been noted that calling unset() does not change the amount of memory allocated at all, but setting vars to NULL actually reduces the amount of memory allocated. Also, unset() has specific behaviors with vars inside of functions and classes, as well as with references.

As I noted above, PHP does what it thinks is best for memory managment. Freeing variable in a larger allocation block is a waste of time in most cases so PHP simply does nothing for better performance.
Weird.. I was always told otherwise, as well from personal experiences.

I would like like a concrete answer if anyone can say for sure :oops:

Posted: Mon Jun 12, 2006 10:30 pm
by bg
if you're coming from a c background, about the only thing similar in both languages is basic syntax. being as php is loosely typed and very high-level, it does a lot of its own decision making. you don't have to hold its hand through memory allocation, it cleans up after itself at the end of each script.

Posted: Mon Jun 12, 2006 11:41 pm
by Christopher
Jcart wrote:I would like like a concrete answer if anyone can say for sure :oops:
I would too. I did a few searches and found very little.

Posted: Tue Jun 13, 2006 3:08 am
by Maugrim_The_Reaper
Here's an example I came across...

Code: Select all

$a = "test";
$b =& $a;
$c =& $b;

unset( $a );

print( "$b $c" );
$a is the variable containing value "test". $b and $c are references. When you call unset() against $a, $b and $c STILL point to the "test" values which has not been removed from memory. In other words, unset()'ing a variable with references does not remove the value from memory. Since Objects are passed all over an app by reference it makes you wonder if unset() is worth using... Anyone come across anything else?

Code: Select all

<?php

class Test {

	public $value = 'I am Test!';

	public function __construct() {}

}

class Test2 {

	public $test_ref = null;

	public function __construct($test_ref) {
		$this->test_ref = $test_ref;
	}
}

$a = new Test;
$b = new Test2($a);
unset($a);
var_dump($b->test_ref); // Test has survived unscathed

?>