-
Notifications
You must be signed in to change notification settings - Fork 72
Description
Hi there!
I'll get straight to the point, consider this simple example:
echo memory_get_usage() / 1024 / 1024 . ' MB' . PHP_EOL; // 1.11 MB
for ($i = 0; $i < 100000; $i++) {
$array[] = new MyObject('string1', 'string2');
}
echo memory_get_usage() / 1024 / 1024 . ' MB' . PHP_EOL; // 11.73 MB <--- Memory usage after initial array of objects is created
igbinary_serialize($array); // <--- results not saved on purpose
echo memory_get_usage() / 1024 / 1024 . ' MB' . PHP_EOL; // 47.59 MB <--- Memory usage after simply calling igbinary_serialize
unset($array);
echo memory_get_usage() / 1024 / 1024 . ' MB' . PHP_EOL; // 2.1 MB <--- Memory usage after the original array is unsetAs you can see, simply calling igbinary_serialize modifies the memory footprint of the original object.
Now this does not happen with arrays at all, so if we use
for ($i = 0; $i < 100000; $i++) {
$array[] = ['string1', 'string2'];
}The results will be:
Initial memory usage: 1.11 MB
After array is created: 3.1159133911133 MB
After igbinary_serialize: 3.1159133911133 MB (absolutely 0 bytes changed)
After `unset($array)`: 1.11 MB (back to initial value)
Is there something that can be done, or am I going crazy? 😄 Thanks a bunch!
P.S.
When using PHPs serialize, everything is fine and memory usage does not increase, though after unserializing, the objects take up more memory because there is no string compacting. So would be cool if this could be fixed in igbinary 😢
P.P.S
Found a hacky workaround to prevent the memory footprint from increasing, so if I do igbinary_serialize(unserialize(serialize($array))), the memory usage does not grow, because I am no longer serializing the original object. But man it feels terrible 😬