Last active
April 3, 2024 06:55
-
-
Save brandonpayton/934d96d76b96557e94ad6983aaffa09f to your computer and use it in GitHub Desktop.
Script that revealed OOM condition due to memory leak
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', '1G'); | |
function useAllMemory() { | |
echo "Initial memory limit: " . ini_get( 'memory_limit' ) . '<br>'; | |
echo "Initial memory usage: " . (memory_get_usage()/(1024*1024)) . ' MB <br>'; | |
echo "<br>"; | |
$data = ''; | |
$counter = 0; | |
$total_strlen = 0; | |
$tail = str_repeat('a', 64 * 1024); // Increase string size by 64KB in each iteration | |
for ($counter = 0; $counter < 1000; $counter++) { | |
$data .= $tail; | |
$usage = memory_get_usage(); | |
$real_usage = memory_get_usage(true); | |
$strlen = strlen($data); | |
$total_strlen += $strlen; | |
echo "* iteration: $counter <br>"; | |
echo "* strlen(): " . ($strlen/(1024*1024)) . " MB <br>"; | |
echo "* memory_get_usage(false): " . ($usage/(1024*1024)) . " MB <br>"; | |
echo "* memory_get_usage(true): " . ($real_usage/(1024*1024)) . " MB <br>"; | |
echo "* aggregate strlen()'s: " . ($total_strlen/(1024*1024)) . " MB <br>"; | |
echo "<br>"; | |
} | |
} | |
useAllMemory(); | |
die(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment