[php-src] Issue #8896: Magic with memory usage or array
| From: | woodholly | Date: | Thu, 30 Jun 2022 00:39:58 +0000 |
| Subject: | [php-src] Issue #8896: Magic with memory usage or array | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-241837@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8896
Author: woodholly
### Description
The following code:
```php
<?php
$a = array();
for($i=1; $i<=1000000;$i++) {
$a[$i] = var_export(array("foobar$i"), true);
}
echo "done, memory usage = " . (memory_get_usage(true)/1024/1024) . "Mb\n";
```
Resulted in this output:
```
done, memory usage = 278.00390625Mb
```
But this code with var_export casted to "real" string:
```php
<?php
$a = array();
for($i=1; $i<=1000000;$i++) {
$a[$i] = "" . var_export(array("foob(ar$i"), true);
}
echo "done, memory usage = " . (memory_get_usage(true)/1024/1024) . "Mb\n";
```
Resulted in this output:
```
done, memory usage = 94.00390625Mb
```
1) Why original var_export eats x3 more memory in comparison with string-casted variant ? (same
thing with json_encode)
2) Why casting using (string) does not help ?
What kind of magic is working here ?
### PHP Version
PHP 7.4
### Operating System
Ubuntu