How to release memory in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ashly
Forum Newbie
Posts: 9
Joined: Thu Dec 21, 2006 12:14 am

How to release memory in PHP

Post by ashly »

Can anyone explain how to free the memory in PHP script after a block of code is executed?
I am using PHP5 on apache server
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You don't need to worry about it for the mostpart. PHP does its own garbage collection much like other high-level languages, however, you can call unset(). Before you call unset() however, set the value of variable to null because PHP may not remove it from memory at the time you call unset(), it will simply clear it from the namespace.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Just be careful setting null when a variable is an object before unsetting. There's times where you just want to destroy an object reference to reuse the variable name without effecting other references to the original memory value. I suppose knowing the difference is one of those key pieces of information you need, esp. for PHP5.
Post Reply