Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>The golden rule</h2> <p>The number one thing to do when you encounter (or expect to encounter) memory pressure is: <strong>do not read massive amounts of data in memory at once if you intend to process them sequentially</strong>.</p> <p>Examples:</p> <ul> <li>Do not fetch a large result set in memory as an array; instead, fetch each row in turn and process it before fetching the next</li> <li>Do not read large text files in memory (e.g. with <code>file</code>); instead, read one line at a time</li> </ul> <p>This is not always the most convenient thing in PHP (arrays don't cut it, and there is <em>a lot</em> of code that only works on arrays), but in recent versions and especially after the introduction of <a href="http://php.net/manual/en/language.generators.overview.php" rel="noreferrer">generators</a> it's easier than ever to stream your data instead of chunking it.</p> <p>Following this practice religiously will "automatically" take care of other things for you as well:</p> <ul> <li>There is no longer any need to clean up resources with a big memory footprint by closing them and losing all references to them on purpose, because there will be no such resources to begin with</li> <li>There is no longer a need to unset large variables after you are done with them, because there will be no such variables as well</li> </ul> <h2>Other things to do</h2> <ul> <li>Be careful of creating closures inside loops; this should be easy to do, as creating such inside loops is a bad code smell. You can always lift the closure upwards and give it more parameters.</li> <li>When expecting massive input, design your program and pick algorithms accordingly. For example, you can mergesort any amount of text files of any size using a constant amount of memory.</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