Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Huge array takes more memory space than it should

Tags:

arrays

php

memory

Currently my app uses around 7 MB memory.

The array appears to use 700 KB if I check it size with strlen(serialize($array)))).

  • If use this array within my app, and check memory usage (with memory_get_peak_usage()) I get 14 MB.

  • If I don't 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 the benchmark the wrong way?

like image 470
ellabeauty Avatar asked Aug 11 '12 23:08

ellabeauty


2 Answers

You wanna steer 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 just like some living space.

When possible you could use SplFixedArray, but then again who cares about what space a PHP array takes. If you are looking for clean / performance stuff why are you using PHP in the first place (yes this is coming from a PHP guy) :)

like image 170
PeeHaa Avatar answered Oct 15 '22 00:10

PeeHaa


Serialized formats can do some memory optimization because they do not need to have the object exist in memory and to have it fully accessibility. They only keep the integrity of the data, not it's accessibility. If this helps to answer your question a little.

like image 21
hakre Avatar answered Oct 14 '22 23:10

hakre