<?php
$memoryLimit = ini_get('memory_limit');
header('Content-Type: text/plain');
echo 'Reported memory limit is: ' . $memoryLimit . PHP_EOL;
$memoryTarget = 0;
// Attempt to massage out what this memory limit really is
if (substr($memoryLimit, -1, 1) == 'M') {
// MEGA
$memoryTarget = substr($memoryLimit, 0, -1) * 1024 * 1024;
} else if (substr($memoryLimit, -1, 1) == 'K') {
// KILO
} else {
echo 'Sorry script does not support the memory limit';
exit;
}
echo $memoryTarget;
$memoryBase = round(memory_get_usage() / 1024 / 1024, 2);
echo 'Base point -> ' . memory_get_usage() . ' (' . $memoryBase . ' MB)' . PHP_EOL;
$pattern = str_repeat('0123456789', 1000);
$fill = str_repeat($pattern, 1);
$counter = 1000;
while ( $counter <= $memoryTarget / 10000) {
unset($fill);
$memoryBase = round(memory_get_usage() / 1024 / 1024, 2);
echo 'Clear -> ' . memory_get_usage() . ' (' . $memoryBase . ' MB)' . PHP_EOL;
echo $counter . ' x 1000 chars -> ';
$fill = str_repeat($pattern, $counter);
$memoryBase = round(memory_get_usage() / 1024 / 1024, 2);
echo memory_get_usage() . ' (' . $memoryBase . ' MB)'. ' [' . $memory_pct = round($memoryBase / ($memoryTarget / 1024/1024) *100 , 1) . ' percent]' . PHP_EOL;
$counter += 500;
}
echo PHP_EOL . 'If you can see this, the server allocated something close to ' . $memoryTarget . ' bytes' . PHP_EOL;
preferences:
30.51 ms | 402 KiB | 5 Q