note 105980 added to function.unset

From: Date: Thu, 29 Sep 2011 23:56:35 +0000
Subject: note 105980 added to function.unset
Groups: php.notes 
Request: Send a blank email to php-notes+get-183531@lists.php.net to get a copy of this message
Despite much searching, I have not yet found an explanation as to how one can manually free resources from variables, not so much objects, in PHP. I have also seen many comments regarding the merits and demerits of unset() versus setting a variable to null. Thus, here are the results of some benchmarks performed comparing unset() of numerous variables to setting them to null (with regards to memory usage and processing time): 10 variables: Unset: Memory Usage: 296 Time Elapsed: 1.0013580322266E-5 Null set: Memory Usage: 1736 Time Elapsed: 5.9604644775391E-6 50 variables: Unset: Memory Usage: 296 Time Elapsed: 3.6001205444336E-5 Null set: Memory Usage: 8328 Time Elapsed: 3.2901763916016E-5 100 variables: Unset: Memory Usage: 296 Time Elapsed: 5.6982040405273E-5 Null set: Memory Usage: 15928 Time Elapsed: 5.8174133300781E-5 1000 variables: Unset: Memory Usage: 296 Time Elapsed: 0.00041294097900391 Null set: Memory Usage: 168096 Time Elapsed: 0.00067591667175293 10000 variables: Unset: Memory Usage: 296 Time Elapsed: 0.0042569637298584 Null set: Memory Usage: 1650848 Time Elapsed: 0.0076930522918701 100000 variables: Unset: Memory Usage: 296 Time Elapsed: 0.042603969573975 Null set: Memory Usage: 16249080 Time Elapsed: 0.087724924087524 300000 variables: Unset: Memory Usage: 296 Time Elapsed: 0.13177299499512 Null set: Memory Usage: 49796320 Time Elapsed: 0.28617882728577 Perhaps my test code for the null set was flawed, but despite that possibility it is simple to see that unset() has minimal processing time impact, and no apparent memory usage impact (unless the values returned by memory_get_usage() are flawed). If you truly care about the ~4 microseconds saved over <50 variables, more power to you. Otherwise, use unset() to minimize script impact on your system. Note: Tested on PHP 5.3.8 installed via RPM on Fedora 14 ---- Server IP: 208.69.120.55 Probable Submitter: 70.91.236.105 ---- Manual Page -- http://www.php.net/manual/en/function.unset.php Edit -- https://master.php.net/note/edit/105980 Del: integrated -- https://master.php.net/note/delete/105980/integrated Del: useless -- https://master.php.net/note/delete/105980/useless Del: bad code -- https://master.php.net/note/delete/105980/bad+code Del: spam -- https://master.php.net/note/delete/105980/spam Del: non-english -- https://master.php.net/note/delete/105980/non-english Del: in docs -- https://master.php.net/note/delete/105980/in+docs Del: other reasons-- https://master.php.net/note/delete/105980 Reject -- https://master.php.net/note/reject/105980 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#183531) next »