How to look for PHP memory leaks?
Moderator: General Moderators
How to look for PHP memory leaks?
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.
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.
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Thats what I was wondering...feyd wrote:what's your definition of memory leaks?
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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Good point. Should have made that clearer.what's your definition of memory leaks?
Thanks, I'll have a look at it.I think you may be looking for XDebug.
This is partly what I was aiming at.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
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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.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.
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)
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Interestingly you can unset cast in PHP.Jcart wrote:unset removes the variable from memory, setting it to null keeps it in memory
Code: Select all
(unset) $foo; //Casts as null- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
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.Jcart wrote:unset removes the variable from memory, setting it to null keeps it in memory
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)
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
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.
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Weird.. I was always told otherwise, as well from personal experiences.arborint wrote: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.Jcart wrote:unset removes the variable from memory, setting it to null keeps it in memory
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.
I would like like a concrete answer if anyone can say for sure
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
Here's an example I came across...
$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
$a = "test";
$b =& $a;
$c =& $b;
unset( $a );
print( "$b $c" );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
?>