Skip to content

Instantly share code, notes, and snippets.

@AllenJB
Created December 3, 2022 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AllenJB/8b811ca4ffe20f2fea32c1673c82c494 to your computer and use it in GitHub Desktop.
Save AllenJB/8b811ca4ffe20f2fea32c1673c82c494 to your computer and use it in GitHub Desktop.
PHP: Change memory_limit in shutdown handler after OOM
<?php
ini_set('memory_limit', '2M');
function shutdownHandler()
{
print "Shutdown handler triggered\n";
print "Memory limit: ". ini_get('memory_limit') ."\n";
// If the next line is commented, a second OOM will be triggered
ini_set('memory_limit', '-1');
print "Memory limit: ". ini_get('memory_limit') ."\n";
print "Mem usage: ". memory_get_usage(true) ."\n";
$a = '';
$a .= str_repeat("Hello", 1024 * 1024);
print "Mem usage: ". memory_get_usage(true) ."\n";
}
register_shutdown_function('shutdownHandler');
print "Memory Limit is: ". ini_get('memory_limit');
$a = '';
while (true) {
$a .= str_repeat("Hello", 1024 * 1024);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment