Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In general, you shouldn't need to manually manage memory in PHP, as it has a high-level Memory Manager built in to the Zend Engine which takes care of this for you. However, it is useful to know a bit about how this works in order to better understand why your code is running out of memory.</p> <p>As <strong>a very basic overview</strong>, PHP frees memory based on a "refcount" of how many variables are referencing a particular piece of data. So if you say <code>$a = 'hello'; $b = $a;</code>, a single piece of memory containing the string <code>'hello'</code> will have a refcount of 2. If you call <code>unset()</code> on either variable, or they fall out of scope (e.g. at the end of the function they were defined in), the refcount will decrease. Once the refcount reaches zero, the data will be deleted and the memory freed. Note that "freed" in this case means freed for use by other parts of that PHP script, not necessarily freed back to the Operating System for use by other processes.</p> <p>There are a few <strong>differences between PHP versions</strong> worth knowing:</p> <ul> <li>The reference counting mechanism described above doesn't work if you have <em>circular references</em> (e.g. <code>$obj1-&gt;foo = $obj2; $obj2-&gt;bar = $obj1;</code>) because the reference count never reaches zero. In PHP 5.2 and earlier, this meant that such circular references led to memory leaks, and had to be manually handled by the programmer. In PHP 5.3, a <a href="http://www.php.net/manual/en/features.gc.php" rel="nofollow">"Garbage Collector"</a> was added <em>specifically to handle this case</em>. It does not replace the normal refcount mechanism, but if circular references are common in your code, it may be worth reading up on.</li> <li>PHP 5.4 included a large number of optimizations to the way PHP allocates and uses memory. AFAIK, none of these change the fundamental recommendations of how to write efficient code, they are just a good reason to upgrade your PHP version if you can.</li> </ul> <p>Other than that, there are a few common tips for writing PHP code that makes good use of memory:</p> <ul> <li>Make sure unused variables are discarded when no longer needed. In a well-structured program, this is often a non-issue, because most variables will be local to a particular function; when the function exits, they will go out of scope, and be freed. But if you are creating large intermediate variables, or dynamically creating large numbers of variables, manually calling <code>unset()</code> may be a good idea. And if your code is very linear, or uses large numbers of global and static variables, just refactoring it into a more modular structure may improve its memory performance as well as its readability, maintainability, etc.</li> <li>Assigning or passing a variable by reference (<code>$foo = &amp;$bar</code>) may cause PHP to use <em>more</em> memory than a straight assignment (<code>$foo = $bar</code>). This is because PHP uses a "Copy On Write" mechanism to to store variables with the same content in one location of memory, but reference assignment conflicts with this mechanism, so PHP has to copy the variable early.</li> <li>Objects are more memory-hungry than scalar values (int, boolean, string) or arrays. This is one of the things that has been much improved in PHP 5.4, but is still worth thinking about - although obviously not to the exclusion of writing well-structured code!</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload