utexas_pjm Posted February 15, 2007 Share Posted February 15, 2007 I'm not able to find any reliable information of this subject, so I was wondering of any of you could help me out. Most PHP applications are written for use within a webserver so the application is interpreted and executed then its memory is freed, this makes memory management not much of a concern for the developer. However, I am in the early development stages of a daemon type PHP app (my recent post in the Future Contest Ideas thread sparked my curiosity) and I am now concerned with the possibility of memory leaks which brings me to my question: Does PHP5 employ garbage collection? In Java the garbage collection will release all memory on the heap which is no longer referenced by any references still in scope. That is, I can do something like this without eventually running out of memory: //... public void listen() { Foo fooInstance; while(true){ fooInstance = new Foo(); fooInstance.doSomething(); fooInstance = null; } } //... Would this work in PHP? Or do I need to explicitly release the memory on the heap ala C? If someone could lend me some insight, or even better, point me in the direction of some reliable documentation I would greatly appreciate it. Best, Patrick Quote Link to comment Share on other sites More sharing options...
fert Posted February 16, 2007 Share Posted February 16, 2007 PHP does have garbage collection Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted February 16, 2007 Author Share Posted February 16, 2007 Thanks fert, I finally sucked it up and ran some tests on my own. PHP (5.1) reacts as I expected it would (should). <?php class Foo { private $largeArray; public function __construct() { for($i = 0; $i < 10000; ++$i){ $this->largeArray[] = rand(10000, 1000000); } } public function __destruct() { print "Poof\n"; } } for($i = 0; $i < 3; ++$i){ print "Initial\t\t\t" . memory_get_usage()/1024 . "k\n"; $fooInstance = new Foo(); print "After instanciation\t" . memory_get_usage()/1024 . "k\n"; } ?> Results in: Initial 43.3984375k After instanciation 654.5625k Initial 654.671875k Poof After instanciation 668.640625k Initial 668.640625k Poof After instanciation 668.640625k Poof Best, Patrick Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.