2

Im making a webinterface on a Raspberry Pi, and because of its limited ressources i need to get as much free ram as possible. My question is if there is any benefit in running unset($someClass) at the end of each php page to gain some ram or is it just a waste of time?

Example:

<?php
session_start();

$fh = new FileHandler();

// .. lots of code

$fh->classFunctionUsage($blabla);

// .. more code and then the end of the file
// should i do:
unset($fh);
// .. or not ?
Daniel Jørgensen
  • 1,183
  • 2
  • 19
  • 42
  • 6
    at the end of script execution php automatically cleans all memory used by this script, anyway - sometimes it makes sense to unset objects in the middle of your script execution – Iłya Bursov Mar 14 '16 at 20:57
  • If you're having trouble with running out of memory then bench it, figure out where your application consumes a lot of memory and try to limit it (re-bench to see if your fix works). If you don't have any problems - then don't fix it ^^ – JimL Mar 14 '16 at 21:14
  • Understand that unset does not immediately free the memory. The garbage collector will free it at an appropriate time. You will see more immediate effects by setting the variable to NULL first, but the garbage collector does know what it's doing anyway, in theory. Have a read of this http://stackoverflow.com/questions/584960/whats-better-at-freeing-memory-with-php-unset-or-var-null – rjdown Mar 14 '16 at 21:21
  • Thanks for the comments! This leaves me with the decision of not adding unset anywhere as i assume php will do its thing. Im not running out of memory or anything, but i just wanted to tidy up the application. @JimL can you point me in any direction on how to do a benchmark ? – Daniel Jørgensen Mar 14 '16 at 21:50
  • @DanielJørgensen http://stackoverflow.com/questions/8291366/how-to-benchmark-efficiency-of-php-script – JimL Mar 14 '16 at 21:55

0 Answers0