0

I have a loop which is something like

    $items        = array(10, 12, 13, 23, 34, 32, 11, 98);
    $items_single = array();

    foreach ($items as $item) {
        $item_data = "SELECT * FROM MY_TABLE WHERE ITEM_ID = $item";
        // Don't worry, this is just a demo query, I don't query like this

        array_push($items_single, $item_data);

        unset($item_data);
//      $item_data = NULL;
    }

Which is a better way to free the $item_data memory? While unset() seems more logical, but I think this way php is not going to unset it right away, but will put it garabage collection sort of thing which might be called after the loop...? (which will fail the purpose of having unset() in the first place). While = NULL seems to be doing what would be ideal, but then it will take some constant time which might actually slow down the looping.

Also, if I don't do anything at all, then php will create a new variable every time (which will consume memory), and then try to remove my old one and replace with the new one (I guess it works this way).

J D
  • 1,768
  • 2
  • 18
  • 20
  • 4
    Don't worry about such cases! The PHP GC is probably clever enough to figure out that you don't want to re-create a variable. If you really need to, run a (correct) benchmark at least! – ComFreek Nov 14 '13 at 16:00
  • 2
    See [this answer](http://stackoverflow.com/a/13667734/1438393) for a benchmark. – Amal Murali Nov 14 '13 at 16:02
  • 1
    Your last paragraph is wrong. Although defined in this "scope", `$item_data` will survive each iteration, so not unsetting at all is a possibility as well – kero Nov 14 '13 at 16:02
  • 2
    $item_data is overwritten each time, so setting it to null is useless. The only thing here that might be useful is to unset the variable (or make it null) after the loop, because otherwise it will keep to exist for the rest of the script (if it's global). But then, that is only useful if it is a big chunk of data. In normal circumstances you don't need to clean up a variable like this yourself. – GolezTrol Nov 14 '13 at 16:05
  • 1
    How big the select query possibly can be? Otherwise it's quite pointless to unset the variable inside the loop, as for next iteration it will be overwritten. – art2 Nov 14 '13 at 16:09
  • @art2 The Select query is quite huge and will return couple of hundred to thousands of rows. This is why I thought it may be a good idea to get rid of it when I'm done. – J D Nov 14 '13 at 16:10
  • @ComFreek and @GolezTrol So you mean having `unset` or `= NULL` is silly inside a loop? – J D Nov 14 '13 at 16:11
  • 1
    @JohnSmith Is `$item_data` the result data? If so, I would profile the code with and without the `unset()/=NULL` if I were you. I once got in a situation where unsetting a SimpleHTMLDOM object (it's from a library) made the script execution noticeable faster! – ComFreek Nov 14 '13 at 16:15
  • Okay, I ran speed tests and as expected, having neither `unset` and `=NULL` was faster. Also, `=NULL` was much faster than `unset`. – J D Nov 14 '13 at 16:36
  • 1
    Yes exactly. A hundeds to thousands of rows for the variable isn't that much to effect drastically on performance. But if you think you need to free up the space then NULL should be enough, as it only sets the variable to null while unset destroys the whole variable from memory, and in the loop the variable needs to be initialized again anyway. – art2 Nov 14 '13 at 16:42

0 Answers0