- str_repeat: documentation ( source)
- memory_get_usage: documentation ( source)
- ini_set: documentation ( source)
- sprintf: documentation ( source)
<?php
echo sprintf("\n%.1fMB", memory_get_usage(true) / 1024 / 1024);
// allocate a large byte string of around 5 MB
$a = str_repeat("0", 5 * 1024 * 1024);
echo sprintf("\n%.1fMB", memory_get_usage(true) / 1024 / 1024);
// setting the memory limit lower than the current is accepted
ini_set("memory_limit", "3M");
echo sprintf("\n%.1fMB", memory_get_usage(true) / 1024 / 1024);
// further allocation beyond the limit
$b = str_repeat("0", 5 * 1024 * 1024);
echo sprintf("\n%.1fMB", memory_get_usage(true) / 1024 / 1024);