php memory questions

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm using php 4.3, and I"m wondering when is it a good idea to use this
unset() function to free a variable?

Also, what happens internally when a variable is set in one place, then
reset in another -- for instance:

$a = "some string";

and later on in the script I have:

$a = "a different string";

What happens to "some string" -- is it still floating around in memory
(without any references to it)?  Or is it garbage collected & memory
freed?  Should I use unset($a) to free "some string" before resetting
$a to something different?


Re: php memory questions wrote:

Quoted text here. Click to load it

It rather depends what for and where you're using it.

Assuming its for generating web pages, called as CGI, fast CGI or module,
then don't worry - the runtime memory gets blown away after servicing each

IME PHP does a very good job of tracking what memory it has allocated - I've
run servers fielding millions of hits per day and only had to take them
down for patching.


Re: php memory questions

For memory variables you shouldnt have to do anything. PHP handles
everything through garbage collection.

But for database resources, sometimes you may have to. E.g. handles to
databases and tables. If you don't, the handle gets destroyed but the
allocated resource is still there in memory and repeated calls to
connect/execute you will have a memory leak.

Always close handles.

Colin McKinnon wrote:
Quoted text here. Click to load it

Site Timeline