php memory de-allocation

Soldato
Joined
17 Jul 2008
Posts
2,820
Location
London
So after doing a spot of googling, I haven't found a good answer for this.

Got a script that runs daily on a client that operates on 500 records at a time. It fetches them and stores them in an object. What's the sensible way of clearing this object? What it does is this:

Code:
$obj = fetch500
while(more_records) {
   $obj = fetch500
   doStuff
}

Which is fine since the obj gets overwritten inside the while loop, but after the loop finishes, I want to clear the memory of this object. Should I use unset or set the object to NULL? Or something else that I am not aware of?

Yes I can increase memory limits... but I would prefer not to have a massive memory intensive script running daily xD.
 
Unless your server is an Amstrad CPC 464, 500 records is nothing at all. The inbuilt garbage collector would be more than capable of handling that.
 
Oh yeah its not that much but it is for the environment in question. And I would rather improve the script rather then blindly increase memory limits. Those records generate a 15-20mb object when loaded (there are other overheads as we have to do soap calls and such and such, but anyway that's not the issue here.

Was mostly asking for a sensible way to clear memory used by the variable (or mark it up for garbage collection) that is no longer needed while staying in the same function scope.
 
Last edited:
Worried about micro-managing the memory? Stop using PHP.

This is a complete non-issue. Unset your variables/references if you care to, and the garbage collector will do the rest. Not to mention all resources are killed once the script completes anyway. I cannot emphasise enough just how little this actually matters.
 
From my understanding (could be wrong) - UNSET just removes the variable/object from the current scope and won't necessarily free up the variable/objects allocated memory (not until garbage collection, which i believe runs during idle cycles); whereas setting a variable/object to NULL clears the data, inturn freeing up allocated memory, but at cost of cpu cycles.

Edit - quick Google - http://stackoverflow.com/questions/584960/whats-better-at-freeing-memory-with-php-unset-or-var-null

So you unset it then call gc_collect_cycles(), I would imagine?

I've never used, probably never will, it but it's there if you really need it.
 
Back
Top Bottom