Thursday 6 June 2013

Re: [dcphp-dev] Memory Usage in PHP Arrays

Ray,

This will answer your question: http://nikic.github.io/2011/12/12/How-big-are-PHP-arrays-really-Hint-BIG.html

The short answer is, PHP has to store some data about the values, which makes it roughly 18 times larger than the actual data.

As for unsetting the string, garbage collection doesn't automatically run on that string. But the array seems to have garbage collection immediately.

Brandon


On Thu, Jun 6, 2013 at 6:58 PM, Ray Paseur <ray.paseur@gmail.com> wrote:
Colleagues:  I'd like some help understanding what is going on here.  Here is a little script that you can copy and run to see the outputs that are annotated in the comments.  Questions follow.

<?php // RAY_temp_julianh.php
error_reporting(E_ALL);
echo '<pre>';

// URL OF THE INPUT FILE
$url = 'http://www.laprbass.com/RAY_temp_julianh.csv';  // FILESIZE IS 10,788,344 BYTES

// CHECK THE MEMORY USAGE
$use = memory_get_usage(TRUE);
var_dump($use);                  // int(786432)

// READ THE FILE INTO A STRING
$str = file_get_contents($url);
$use = memory_get_usage(TRUE);
var_dump($use);                  // int(11796480)

// MAKE AN ARRAY
$arr = explode(PHP_EOL, $str);
$use = memory_get_usage(TRUE);
var_dump($use);                  // int(221773824)

// DELETE THE STRING VARIABLE
unset($str);
$use = memory_get_usage(TRUE);
var_dump($use);                  // int(221773824)

// DELETE THE ARRAY VARIABLE
unset($arr);
$use = memory_get_usage(TRUE);
var_dump($use);                  // int(12058624)

The output of the first var_dump() makes sense, and given the file size, the output of the second var_dump() after reading the file makes sense.  Then I am confused.

The output from the third var_dump() after creating the array seems to indicate that array creation took a 10MB string and multiplied its memory size by nearly a factor of 20.  This caused me to do an immediate WTF, so I tested it by making an object with a property for each element.  Very similar numbers.  Why does it take 20X the storage to represent an array or object?

The fourth var_dump() after unset() of the 10MB input string showed no change in memory usage.  Why was no memory released?  Or is memory_get_usage(TRUE) reporting something that is understood in the art but is opaque to me? For example, is there a minimum memory allocation?

The fifth var_dump() after unset() of the array seems to have released memory associated with the array, but leaving only 256K unaccounted for.  Why?

Thanks for any insight you can share.  Best to all, Ray

--
You received this message because you are subscribed to the Google
Group: "Washington, DC PHP Developers Group" - http://www.dcphp.net
To post, send email to washington-dcphp-group@googlegroups.com
To unsubscribe, send email to washington-dcphp-group+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/washington-dcphp-group?hl=en
---
You received this message because you are subscribed to the Google Groups "Washington, DC PHP Developers Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to washington-dcphp-group+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

--
You received this message because you are subscribed to the Google
Group: "Washington, DC PHP Developers Group" - http://www.dcphp.net
To post, send email to washington-dcphp-group@googlegroups.com
To unsubscribe, send email to washington-dcphp-group+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/washington-dcphp-group?hl=en
---
You received this message because you are subscribed to the Google Groups "Washington, DC PHP Developers Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to washington-dcphp-group+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

0 comments:

Post a Comment