Last active
July 12, 2019 22:33
-
-
Save Danack/69323606f144a2bbb9db to your computer and use it in GitHub Desktop.
An example of how creating cycles in variables leads to
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Without manually calling gc_collect_cycles() | |
============================================ | |
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 139264 bytes) in - on line 27 | |
GC Statistics | |
------------- | |
Runs: 0 | |
Collected: 0 | |
Root buffer length: 254 | |
Root buffer peak: 260 | |
Possible Remove from Marked | |
Root Buffered buffer grey | |
-------- -------- ----------- ------ | |
ZVAL 261 260 6 0 | |
ZOBJ 0 0 0 0 | |
Manually calling gc_collect_cycles() | |
==================================== | |
Max memory that should be 'live' is 5120 kB | |
used: 358688 | allocated: 524288 | peak: 786432 | |
GC Statistics | |
------------- | |
Runs: 1000 | |
Collected: 2000 | |
Root buffer length: 0 | |
Root buffer peak: 5 | |
Possible Remove from Marked | |
Root Buffered buffer grey | |
-------- -------- ----------- ------ | |
ZVAL 1007 1007 6 2002 | |
ZOBJ 0 0 0 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$performGC = false; | |
if ($performGC) { | |
echo "GC collection will be done - app should not crash.\n"; | |
} | |
else { | |
echo "GC collection won't be done - app should crash.\n"; | |
} | |
$dataSizeInKB = 128; | |
//Change this line if you tweak the parameters above. | |
ini_set('memory_limit', "64M"); | |
for ($y=0 ; $y<$dataSizeInKB ; $y++) { | |
for ($x=0 ; $x<32 ; $x++) { //1kB | |
$memData .= md5(time() + (($y * 32) + $x)); | |
} | |
} | |
file_put_contents("memdata.txt", $memData); | |
// This function creates a cyclic variable loop | |
function useSomeMemory($x) { | |
$data = []; | |
$data[$x] = file_get_contents("memdata.txt"); | |
$data[$x + 1] = &$data; | |
}; | |
for($x=0 ; $x<1000 ; $x++) { | |
useSomeMemory($x); | |
if ($performGC == true) { | |
gc_collect_cycles(); | |
} | |
} | |
printf("\nused: %10d | allocated: %10d | peak: %10d\n", | |
memory_get_usage(), | |
memory_get_usage(true), | |
memory_get_peak_usage(true) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment