Log in

No account? Create an account

PHP memory usage

« previous entry | next entry »
Sep. 6th, 2007 | 12:22 pm
location: Home
mood: accomplishedaccomplished
music: Whirring fans

I just discovered a way to reduce peak memory usage in certain php scripts. This is tested with PHP 4.4.6

while (some loop) {
  $data = fetch_big_glob_of_data();
  # Do stuff with the data

What's important here is the unset() at the end. If this is not used, then PHP is required to keep a copy of the old $data until the new $data is fetched. This means two instances are in memory at one time. By explicitly freeing $data when it's no longer needed, PHP can reuse the space immediately.

In my case, peak memory usage dropped from 220MB to 110MB, as $data was around 110MB in size.

Link | Leave a comment |

Comments {0}