A huge array takes up more memory than it should

My application currently uses about 7 MB of memory.

It seems that the array uses 700K if I check its size with strlen(serialize($array)))) .

  • If you use this array in my application and check the memory usage (using memory_get_peak_usage() ), I get 14 MB.

  • If I do not use it, I get 7 MB.

If this array takes 700 KB serialized, why does PHP need 7 MB for this variable? Or am I doing a benchmark incorrectly?

+8
arrays php memory
source share
2 answers

You want to get away from PHP if this bothers you: http://nikic.github.com/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html . PHP arrays, like some living spaces.

When possible, you can use SplFixedArray , but again, who cares about how much space the PHP array occupies. If you are looking for clean / efficient things, why are you using PHP in the first place (yes, this comes from the PHP guy) :)

+10
source share

Serialized formats can perform some memory optimization because they do not need the object to exist in memory and have full accessibility. They preserve data integrity, not availability. If this helps answer your question a little.

+1
source share

All Articles