PHP TIP: Unset() : Memory Usage!

Status
Not open for further replies.

litewarez

Active Member
1,367
2008
1
0
This is just a tip for when your building your applications in PHP and why you should use the unset() function.

For unset there are 2 main reason why you would use such a command.

  1. To unset a variable so you can redeclare it later as a fresh variable!
  2. Reduce memory usage dure the execution of your script

Now the main problem in PHP scripts is keeping large variables throught the whole execution of the script, causing the script to slow down dramatically after the variable is set..

here is an example of not using unset and the ram levels changing.

PHP:
//First we check the state of the memory at the start.

echo memory_get_peak_usage() . "\n"; // 36640

/*
    * as you can see above the ram usage is at 36640.. 36.6Mb
    * but dont forget the 36.6Mb is also apache,mysql and other services
*/

//Lets run a test to see if we can get it to peak

$a = str_repeat("wjunction", 4000); // repeat a string 4000 times


//Lets check what effect that above has had on the memory usage
echo memory_get_peak_usage() . "\n"; // 60765

/*
    * As you can see the memory is 60.7Mb thats 24.1Mb increase
*/

Now if you carry on coding the memory usage will just grow as the variable $ is still in the memory waiting to be used again...

but if theres no use for $a, so lets take a look at the effect if we unset the variable after we have finished with it..

PHP:
//First we check the state of the memory at the start.

echo memory_get_peak_usage() . "\n"; // 36640

/*
    * as you can see above the ram usage is at 36640.. 36.6Mb
    * but dont forget the 36.6Mb is also apache,mysql and other services
*/

//Lets run a test to see if we can get it to peak

$a = str_repeat("wjunction", 4000); // repeat a string 4000 times

//UNSET HERE
unset($a);

//Lets check what effect that above has had on the memory usage
echo memory_get_peak_usage() . "\n"; // 38230

/*
    * instead of a 24.1Mb increase we only have a 0.2Mb increase
*/

I hope you can see the importance of unset() and how it will help you make your site load a HELL of a lot faster, especially if your new to programming in PHP and you do stuff with code when you don't fully understand what it does to your memory usage.

Peace out :D
 
9 comments
ur right, i completely underestimated this, now i feel like a retard! shit. also closing mysql connections too when they are done. oh well never to late!

Thanks bro
 
Yes Dman but lets say you had a 20 40Mb clustors being used, im sure 14 unsets would not peak the memory and will be better all round..


the reason i tell these tales to you is alot of people leave like resources open suchs mysql cons,and file wrappers etc and they need to be unset for the better performance and cyber you should know better, treat your pc with love my friend because no matter what.. it will always be better that your girl looool
 
You'd have to have a rather large script to be honest.

I only really unset things in huge pieces of code I write. In your usual standard script that isn't made for some huge traffic site you don't really need to do it.

Dman does have a point too. If you use unset a lot it can also make code look messy and sometimes remove vars which would be useful in future.

Apart from those points though, it *can* be good practice on large scales.
 
ok lets say your script run in like a sequence!!

Frist you start up and load constants then a registry then DB ect.. you could unset after each sequence like this


LOAD Config/Constants
LOAD DATABASE - unset(...,...,...,...,...,);
LOAD MIAN CLASS unset(...,...,);

unset takes multiple vats so if you unset the vars after each block of code.. block as like 6 classes for a certain task im sure that would be clean and better than not using it...

Also id rather my site be the best performance than keep my code 100% clean

But both your points are right :)
 
ok lets say your script run in like a sequence!!

Frist you start up and load constants then a registry then DB ect.. you could unset after each sequence like this


LOAD Config/Constants
LOAD DATABASE - unset(...,...,...,...,...,);
LOAD MIAN CLASS unset(...,...,);

unset takes multiple vats so if you unset the vars after each block of code.. block as like 6 classes for a certain task im sure that would be clean and better than not using it...

Also id rather my site be the best performance than keep my code 100% clean

But both your points are right :)

Yes, you can do that. But it will only be a microoptimisation unless done in a large script is what im saying.
 
yea so the best time to use it mis only on LARGE entities then :) but non the less people know now that it can be REALLY Helpfull if used correctly
 
Status
Not open for further replies.
Back
Top