Jump to content

Recommended Posts

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.