PHP memory consumption with Arrays and Objects (update: generators)
Lessons learned: objects need more memory than arrays (+ 2-10 percent) comparing 32bit to 64bit systems, memory consumption increases by 100-230 percent if array values are numeric, don't save them as strings! saving 1M integers takes 200M of memory with PHP on a 64bit plattform (increase by factor 25) using SplFixedArray can reduce memory usage by 20-130 percent avoid big Arrays and Objects in PHP whenever possible (don't use file('big_file') or explode("\n", file_get_contents('big_file')), etc.) use streams whenever possible (fopen, fsockopen, etc.) use generators when available with PHP 5.5 ( RFC ) If you need to save memory temporarily in your script, you can stringify your array (increasing cpu usage and runtime): $a = array(); for ($i=0; $i $a[] = $i; } $a = implode(',', $a); // or $a = json_encode($a); echo number_format(memory_get_usage(true)/1048576, 2)."\n"; // 8.0 M (64bit), 7.5 M (32bit) Here is a small script to sh...