Skip to content

Instantly share code, notes, and snippets.

@AllenJB
Created December 3, 2022 10:35
Show Gist options
  • Select an option

  • Save AllenJB/8b811ca4ffe20f2fea32c1673c82c494 to your computer and use it in GitHub Desktop.

Select an option

Save AllenJB/8b811ca4ffe20f2fea32c1673c82c494 to your computer and use it in GitHub Desktop.

Revisions

  1. AllenJB created this gist Dec 3, 2022.
    24 changes: 24 additions & 0 deletions oom_example.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    <?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);
    }