Created
December 3, 2022 10:35
-
-
Save AllenJB/8b811ca4ffe20f2fea32c1673c82c494 to your computer and use it in GitHub Desktop.
PHP: Change memory_limit in shutdown handler after OOM
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 | |
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