Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // some sort of hack to get httpd.exe size in mb
- function httpd_mb()
- {
- //"httpd.exe","10744","Services","0","112 924 Kb"
- $cmd = 'tasklist /fi "imagename eq httpd.exe" /fo csv /nh'; // no header
- $res = exec($cmd, $lines);
- $kbs = explode(',',$lines[1])[4];
- $kb = preg_replace('/[^0-9]/','',$kbs);
- $mb = round($kb/1024);
- return $mb;
- }
- function info()
- {
- global $data, $chunk_size_kb;
- $c = count($data);
- $httpd_mb = httpd_mb();
- echo 'apache size: ' . $httpd_mb . ' mb, ';
- echo 'memory usage/real usage: ' . round(memory_get_usage()/1024/1024);
- echo '/' . round(memory_get_usage(true)/1024/1024) . ' mb (';
- echo $c . ' items in array, ';
- echo round($chunk_size_kb*$c/1024) . ' mb stored)';
- echo '<br>';
- flush();
- return $httpd_mb;
- }
- function shutdown()
- {
- echo '<br><b>before crash</b>:<br>';
- info();
- }
- // SETTINGS SECTION (feel free to experiment with it)
- ini_set('memory_limit', '1500M'); // memory leaks faster then limit is bigger.
- $chunk_size_kb = 2043; // on my system 2043kb leaks, 2044 frees memory, everything is ok
- $stat_freq = round(100000 / $chunk_size_kb);
- // END OF SETTINGS
- register_shutdown_function('shutdown');
- $data = array();
- $ap = info();
- // if memory already busy (httpd >> 1G), run this block and fail to alloc memory
- $big_chunk_mb = 200;
- if ($ap > 1800-$big_chunk_mb)
- {
- echo "<h3>apache overloaded ($ap mb), trying to alloc $big_chunk_mb mb</h3>";
- $big = str_repeat('1',$big_chunk_mb*1024*1024);
- }
- // main cycle, allocates memory to array of relatively small strings (<2mb)
- for ($i=0; true; $i++)
- {
- //$data[] = random_bytes($chunk_size_kb*1024); // the same result
- $data[] = str_repeat('1',$chunk_size_kb*1024);
- if ($i % $stat_freq == $stat_freq-1)
- info();
- // looks like any fatal error also keeps memory
- //if ($i == 2000)
- // trigger_error('stop', E_USER_ERROR);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement