php.net |  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
Votes:1
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
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-09-07 15:31 UTC] jimmycnw at gmail dot com
Description:
------------
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);

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

// LOADING
$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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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] jani@php.net
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-2022 The PHP Group
All rights reserved.
Last updated: Fri Dec 09 02:05:54 2022 UTC