#46018 [Opn->Fbk]: Memory leak in unserialize of nested arrays

From: Date: Sun, 07 Sep 2008 17:43:02 +0000
Subject: #46018 [Opn->Fbk]: Memory leak in unserialize of nested arrays
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-128763@lists.php.net to get a copy of this message
ID: 46018 Updated by: jani@php.net Reported By: jimmycnw at gmail dot com -Status: Open +Status: Feedback Bug Type: Performance problem Operating System: Linux PHP Version: 5.2.6 New Comment: 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.. Previous Comments: ------------------------------------------------------------------------ [2008-09-07 15:50:54] 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 15:31:07] 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 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=46018&edit=1

« previous php.bugs (#128763) next »