How to look for PHP memory leaks?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

daou
Forum Newbie
Posts: 8
Joined: Sat Jun 10, 2006 12:53 pm

How to look for PHP memory leaks?

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's your definition of memory leaks?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

If your definition of a memory leak is high memory usage, I think you may be looking for XDebug.
daou
Forum Newbie
Posts: 8
Joined: Sat Jun 10, 2006 12:53 pm

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
daou
Forum Newbie
Posts: 8
Joined: Sat Jun 10, 2006 12:53 pm

Post by daou »

How does PHP handle setting an object or array to NULL?
Is it equivalent to calling unset() and setting to NULL?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

unset removes the variable from memory, setting it to null keeps it in memory
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
bg
Forum Contributor
Posts: 157
Joined: Fri Sep 12, 2003 11:01 am

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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

?>
Post Reply