ellabeauty
ellabeauty

Reputation: 2599

Huge array takes more memory space than it should

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 this array takes 700 KB serialized, why does PHP need 7 MB for this variable? Or am I doing the benchmark the wrong way?

Upvotes: 8

Views: 438

Answers (2)

hakre
hakre

Reputation: 197822

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.

Upvotes: 1

PeeHaa
PeeHaa

Reputation: 72672

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) :)

Upvotes: 10

Related Questions