Advertisement
Guest User

Php memory leak

a guest
Nov 1st, 2021
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.87 KB | None | 0 0
  1. <?php
  2.  
  3. // some sort of hack to get httpd.exe size in mb
  4. function httpd_mb()
  5. {
  6.     //"httpd.exe","10744","Services","0","112 924 Kb"
  7.     $cmd = 'tasklist /fi "imagename eq httpd.exe" /fo csv /nh'; // no header
  8.     $res = exec($cmd, $lines);
  9.     $kbs = explode(',',$lines[1])[4];
  10.     $kb = preg_replace('/[^0-9]/','',$kbs);
  11.     $mb = round($kb/1024);
  12.     return $mb;
  13. }
  14.  
  15. function info()
  16. {
  17.     global $data, $chunk_size_kb;
  18.     $c = count($data);
  19.     $httpd_mb = httpd_mb();
  20.     echo 'apache size: ' . $httpd_mb . ' mb, ';
  21.     echo 'memory usage/real usage: ' . round(memory_get_usage()/1024/1024);
  22.     echo '/' . round(memory_get_usage(true)/1024/1024) . ' mb (';
  23.     echo $c . ' items in array, ';
  24.     echo round($chunk_size_kb*$c/1024) . ' mb stored)';
  25.     echo '<br>';
  26.     flush();
  27.     return $httpd_mb;
  28. }
  29.  
  30. function shutdown()
  31. {
  32.     echo '<br><b>before crash</b>:<br>';
  33.     info();
  34. }
  35.  
  36.  
  37. // SETTINGS SECTION (feel free to experiment with it)
  38.  
  39. ini_set('memory_limit', '1500M'); // memory leaks faster then limit is bigger.
  40. $chunk_size_kb = 2043; // on my system 2043kb leaks, 2044 frees memory, everything is ok
  41. $stat_freq = round(100000 / $chunk_size_kb);
  42.  
  43. // END OF SETTINGS
  44.  
  45.  
  46.  
  47.  
  48. register_shutdown_function('shutdown');
  49. $data = array();
  50. $ap = info();
  51.  
  52. // if memory already busy (httpd >> 1G), run this block and fail to alloc memory
  53. $big_chunk_mb = 200;
  54. if ($ap > 1800-$big_chunk_mb)
  55. {
  56.     echo "<h3>apache overloaded ($ap mb), trying to alloc $big_chunk_mb mb</h3>";
  57.     $big = str_repeat('1',$big_chunk_mb*1024*1024);
  58. }
  59.  
  60. // main cycle, allocates memory to array of relatively small strings (<2mb)
  61. for ($i=0; true; $i++)
  62. {
  63.     //$data[] = random_bytes($chunk_size_kb*1024); // the same result
  64.     $data[] = str_repeat('1',$chunk_size_kb*1024);
  65.    
  66.     if ($i % $stat_freq == $stat_freq-1)
  67.         info();
  68.    
  69.     // looks like any fatal error also keeps memory
  70.     //if ($i == 2000)
  71.     //  trigger_error('stop', E_USER_ERROR);
  72. }
  73.  
  74. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement