You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unserialized objects use significantly more memory than ones created with the normal constructor #10126
SomeClass contains some data. new_object() will create an instance, while unserialize_object() will unserialize an instance of exactly the same state (see $s1). When an array of 50k elements is created with one or another, the output for memory usage is as follows:
new_object 10
unserialize_object 43
I.e. the 50k instances created with the constructor use 10M memory, while the unserialized ones 43M. It seems that some data structures that are used while unserializing are not freed. However, when unsetting $arr to destroy the instances, memory consumption will drop to 1M, as expected.
Now consider OtherClass which has the __unserialize method. Nothing fancy, it just sets the attributes as the default handler would do. new_object_other() and unserialize_object_other() replicate the same logic as explained above, but for the new class. Output in this case is
new_object_other 10
unserialize_object_other 10
That is, memory consumption is the same regardless of how the objects are created.
Surplus memory usage grows with the number of attributes.
Tested with 8.1.2 and 8.2.0. Can also be seen on https://3v4l.org/ .
One thing to note from the 3v4l results (and my own testing) is that from at least 5.2.1 through 7.3.11, unserialize_object_other has the same memory usage as unserialize_object (i.e., over-sized); only from 7.4.0 onward does it match new_object[_other].
Usually, declared object properties are stored in a C array (.properties_table), but unserializing materializes the more general HashTable (.properties), and this HashTable amounts for the additional memory usage. Basically the same happens when you var_dump() the object, and the engine deliberately doesn't delete/purge the .propertiesHashTable once it has been materalized for performance reasons.
but unserializing materializes the more general HashTable (.properties), and this HashTable amounts for the additional memory usage.
And using foreach in the __unserialize method above effectively bypasses the Hashtable by directly populating the C form instead (slower, since done property by property)?
And using foreach in the __unserialize method above effectively bypasses the Hashtable by directly populating the C form instead (slower, since done property by property)?
No, not really. Re-opening, since this might deserve closer investigation.
One thing to note from the 3v4l results (and my own testing) is that from at least 5.2.1 through 7.3.11, unserialize_object_other has the same memory usage as unserialize_object (i.e., over-sized); only from 7.4.0 onward does it match new_object[_other].
This is because ::__unserialize() is only supported as of PHP 7.4.0. Previously, the function was not called.
The reason the table is extended beforehand, is because without it a nested deserialisation may cause an array resize, which will invalidate all pointers to the data. So by resizing beforehand all pointers will remain valid.
One solution could be to shrink the hashtable after the deserialisation, but there is unfortunately no API for that.
I'm sorry for commenting without having deeper knowledge on on the topic. Is it possible to create the hash table on demand? like the first time it's needed? So if nothing has changed since serialization and all properties are declared then you wouldn't need the hash table at all.
I've always declared classes instead of arrays/stdClass objects for big arrays to save memory.
Here are the tests I did (PHP 8.5.12 (cli) (built: Nov 14 2026 18:06:26) (ZTS)) :
Test
Method
Time (ms)
Peak (MiB)
Array memory (MiB)
json.php
JSON → stdClass
1178.035
689.10
572.95
json2.php
JSON + hydrate
2036.147
277.11
160.96
serialize.php
Unserialize
1043.250
704.06
527.17
serialize2.php
Unserialize + hydrate
1807.463
345.48
168.59
serialize3.php
__unserialize()
1934.656
345.48
169.92
__unserialize fixes that problem but it's even slower (method call probably)
The copy of the object almost doubles the time. that may or may not be significant - depending on load and size. If it could be optimized without too much sacrifice, then it would be nice to have that.
Activity
MorganLOCode commentedon Dec 18, 2022
One thing to note from the 3v4l results (and my own testing) is that from at least 5.2.1 through 7.3.11, unserialize_object_other has the same memory usage as unserialize_object (i.e., over-sized); only from 7.4.0 onward does it match new_object[_other].
cmb69 commentedon Dec 19, 2022
Usually, declared object properties are stored in a C array (
.properties_table), but unserializing materializes the more generalHashTable(.properties), and thisHashTableamounts for the additional memory usage. Basically the same happens when youvar_dump()the object, and the engine deliberately doesn't delete/purge the.propertiesHashTableonce it has been materalized for performance reasons.MorganLOCode commentedon Dec 20, 2022
And using
foreachin the__unserializemethod above effectively bypasses theHashtableby directly populating the C form instead (slower, since done property by property)?cmb69 commentedon Dec 20, 2022
No, not really. Re-opening, since this might deserve closer investigation.
This is because
::__unserialize()is only supported as of PHP 7.4.0. Previously, the function was not called.czirkoszoltan commentedon Dec 20, 2022
Hi @cmb69 thanks for considering this. In my use case, it is a difference betweem 500M or 1000M memory usage for the scipt.
@MorganLOCode The workaround is pretty fast, fortunately. It does not slow down the script considerably, the difference is marginal.
ndossche commentedon May 7, 2023
There are two places where the unserializer will allocate more memory when not using __unserialize.
First place:
php-src/ext/standard/var_unserializer.re
Line 830 in 82b0537
The reason the table is extended beforehand, is because without it a nested deserialisation may cause an array resize, which will invalidate all pointers to the data. So by resizing beforehand all pointers will remain valid.
One solution could be to shrink the hashtable after the deserialisation, but there is unfortunately no API for that.
Second place:
php-src/ext/standard/var_unserializer.re
Line 825 in 82b0537
This was already hinted at by Christoph. The properties hash table is rebuilt and will be kept in memory.
It seems like at least the first one should be doable to fix. The second one is for performance (speed) reasons.
The reason it doesn't happen with __unserialize is because it overrides the default behaviour and instead collects the properties into an array:
php-src/ext/standard/var_unserializer.re
Lines 793 to 820 in 82b0537
Less_Parser: Add `cache_incremental` option
Unserialize taint paths without a property table
perf(taint): read a location's compact state without a property table #…
npelov commentedon Nov 14, 2026
I'm sorry for commenting without having deeper knowledge on on the topic. Is it possible to create the hash table on demand? like the first time it's needed? So if nothing has changed since serialization and all properties are declared then you wouldn't need the hash table at all.
I've always declared classes instead of arrays/stdClass objects for big arrays to save memory.
Here are the tests I did (PHP 8.5.12 (cli) (built: Nov 14 2026 18:06:26) (ZTS)) :
json.phpjson2.phpserialize.phpserialize2.phpserialize3.php__unserialize()__unserialize fixes that problem but it's even slower (method call probably)
The copy of the object almost doubles the time. that may or may not be significant - depending on load and size. If it could be optimized without too much sacrifice, then it would be nice to have that.
serialize.php
serialize2.php
serialize3.php
common.php
json.php
json2.php