News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Paul Reinheimer's Blog:
Memory usage in PHP
March 22, 2010 @ 12:09:52

Paul Reinheimer has a reminder shown in his latest post about optimizing your scripts - don't forget the basics like unset.

A colleague called me over today for some help with a memory usage issue in his PHP script. The script was performing the basic (but critical) task of importing data, pulling it in from MySQL in large chunks, then exporting it elsewhere. He was receiving the wonderful "Fatal error: Allowed memory size of XXXX bytes exhausted (tried to allocate YY bytes)" error message. [...] I fixed the memory usage exceeded problem with an unset(), at the end of a loop.

The loop was structured such that the large amounts of data were just being held in memory over and over again, resulting in a huge stackup that can easily cause the "Allowed memory size exhausted..." message.

There are better architectures available to avoid the issue entirely (like not storing everything in the array, just to iterate over it later) but they're an issue for a different post. For a very simple base case demonstration of the issue take a look at the simple example.
1 comment voice your opinion now!
unset memory optimize dataset


blog comments powered by Disqus

Similar Posts

Bard Farstad's Blog: Optimizing Apache, PHP and APC for Intel based Mac

Johannes Schluter's Blog: DTraceing around

Alvaro Videla's Blog: A Word About Caching: Memcached and APC

Benjamin Eberlei's Blog: Benchmark and Optimize Zend Framework Performance

SitePoint PHP Blog: Faster PHP Apps - Profile Your Code with Xdebug


Community Events





Don't see your event here?
Let us know!


language api community podcast series code developer introduction list configure symfony wordpress install laravel release library interview application bugfix threedevsandamaybe

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework