Unserialized objects use significantly more memory than ones created with the normal constructor
Description
Consider the following code:
class SomeClass {
public $a = 1;
public $b = 2;
public $c = 3;
public $d = 4;
public $e = 5;
public $f = 6;
}
$s1 = serialize(new SomeClass());
function new_object() {
return new SomeClass();
}
function unserialize_object() {
global $s1;
return unserialize($s1);
}
class OtherClass {
public $a = 1;
public $b = 2;
public $c = 3;
public $d = 4;
public $e = 5;
public $f = 6;
public function __unserialize(array $attrs) {
foreach ($attrs as $name => $value)
$this->$name = $value;
}
}
$s2 = serialize(new OtherClass());
function new_object_other() {
return new OtherClass();
}
function unserialize_object_other() {
global $s2;
return unserialize($s2);
}
foreach ([
'new_object', 'unserialize_object',
'new_object_other', 'unserialize_object_other',
] as $f) {
$arr = [];
for ($i = 0; $i < 50000; ++$i) {
$arr[] = $f();
}
echo $f, " ", memory_get_usage() >> 20, "\n";
}
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/ .
PHP Version
8.2.0
Operating System
No response
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 .properties HashTable once it has been materalized for performance reasons.
but unserializing materializes the more general
HashTable(.properties), and thisHashTableamounts 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
foreachin the__unserializemethod above effectively bypasses theHashtableby 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.
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.
There are two places where the unserializer will allocate more memory when not using __unserialize.
First place: https://github.com/php/php-src/blob/82b05373b1cd9755ec45a548a8754a8acabd173c/ext/standard/var_unserializer.re#L830 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: https://github.com/php/php-src/blob/82b05373b1cd9755ec45a548a8754a8acabd173c/ext/standard/var_unserializer.re#L825 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: https://github.com/php/php-src/blob/82b05373b1cd9755ec45a548a8754a8acabd173c/ext/standard/var_unserializer.re#L793-L820
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.
serialize.php serialize2.php serialize3.php common.php json.php json2.php