<p>I have a habit of keeping my variable usage to a bare minimum.  So I'm wondering if there is any advantage to be gained by the following:</p>

<pre><code>$query = $mysqli-&gt;query('SELECT * FROM `people` ORDER BY `name` ASC LIMIT 0,30');

// Example 1
$query = $query-&gt;fetch_assoc();

// Example 2
$query_r = $query-&gt;fetch_assoc();
$query-&gt;free();
</code></pre>

<p>Now if I'm right Example 1 should be more efficient as <code>$query</code> is <code>unset</code> when I reassign it  which should free any memory associated with it. However there is a method (<code>MySQLi_Result::free()</code>) which frees associated memory - is this the same thing?</p>

<p>If I don't call <code>::free()</code> to free any memory associated with the result but <code>unset</code> it by reassigning the variable am I doing the same thing? I don't know how to log this sort of thing - does anyone have some ideas?</p>

[{'answer_id': 26537, 'author': 'Brian Warshaw', 'author_id': 1344, 'author_profile': 'https://Stackoverflow.com/users/1344', 'pm_score': 4, 'selected': True, 'text': '<p>The <a href="http://us2.php.net/manual/en/mysqli-result.free.php" rel="noreferrer">manual</a> seems to suggest that you should still be using <code>free()</code> to release the memory. I believe the reasoning is that <code>free()</code> is freeing the memory in <em>MySQL</em>, not in PHP. Since PHP can\'t garbage-collect for MySQL, you need to call <code>free()</code>.</p>\n'}, {'answer_id': 31903, 'author': 'John Douthat', 'author_id': 2774, 'author_profile': 'https://Stackoverflow.com/users/2774', 'pm_score': 2, 'selected': False, 'text': "<p>Example 1 <strong>dissociates</strong> the $query variable from the MySQL result. The MySQL result still exists in memory, and will continue to exist and waste memory until garbage collection occurs.</p>\n\n<p>Example 2 <strong>frees</strong> the MySQL result immediately, releasing the used resources.</p>\n\n<p>However, since PHP pages are generally short-lived with small result-sets, the memory saved is trivial. You will not notice a slowdown unless you leave <strong>a ton</strong> of results in memory over a long period of time on pages that run for a long time.</p>\n\n<p>Brian,\nPHP <em>can</em> garbage collect the MySQL result, it just doesn't happen immediately.\nThe result lives in PHP's memory pool, not in the MySQL server's.</p>\n\n<p>(the locality of memory when using unbuffered queries is slightly different, but they're so rarely used in PHP as to not be worth mentioning)  </p>\n"}]
['https://Stackoverflow.com/questions/26515', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2025/']