Last active
April 13, 2017 08:23
-
-
Save Danack/6bd30de6247bc5148986 to your computer and use it in GitHub Desktop.
PHP memory recursive.
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"); | |
$memData = ''; | |
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