Skip to content

Instantly share code, notes, and snippets.

@Danack
Last active April 13, 2017 08:23
Show Gist options
  • Save Danack/6bd30de6247bc5148986 to your computer and use it in GitHub Desktop.
Save Danack/6bd30de6247bc5148986 to your computer and use it in GitHub Desktop.
PHP memory recursive.
<?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