|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46018 Memory leak in unserialize of nested arrays
Submitted: 2008-09-07 15:31 UTC Modified: 2008-09-15 01:00 UTC
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: jimmycnw at gmail dot com Assigned:
Status: No Feedback Package: Performance problem
PHP Version: 5.2.6 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: jimmycnw at gmail dot com
New email:
PHP Version: OS:


 [2008-09-07 15:31 UTC] jimmycnw at gmail dot com
The code is attached. Try executing 'SAVING' first. It will create a file 'temp.txt' approximately 450Kb in size. Then execute 'LOADING'. You will see, that over 3.5Mb (!!) of memory used in unserialize() function, while there are just 450Kb of data.

Is it a bug in PHP? Any suggestions on how to fix it?

(When I do the same with string, not array, everything works fine. The problem appears only when I use nested arrays)

Reproduce code:
define ('FILENAME', 'temp.txt');
define ('SIZE', 10 * 1024);

$array = array ();
for ($i = 0; $i<SIZE; $i++)
	$array[] = array (uniqid ($i) => array ());
$data = serialize ($array);
file_put_contents (FILENAME, $data);

$before = memory_get_usage ();
$array = unserialize (file_get_contents (FILENAME));
$after = memory_get_usage ();
echo 'Memory used: '.($after-$before).' bytes, while size of data: '.strlen (serialize ($array));

Expected result:
Memory used should be around 450Kb, while I see 3.5Mb.

Actual result:
Memory used: 3.5Mb


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-07 15:50 UTC] jimmycnw at gmail dot com
Execute SAVING part first, in separate script. Then execute LOADING in another script. DO it separately, not in one script.
 [2008-09-07 17:43 UTC]
Exactly where do you get the idea that memory usage should be same as the file size is?? Arrays just take more memory, that's not any bug..
 [2008-09-15 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Tue Jan 31 00:05:52 2023 UTC