SHARE
TWEET

Untitled

a guest May 24th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     public function memoryUsage()
  2.     {
  3.         $total = 0;
  4.         $free = 0;
  5.         $memInfo = '/proc/meminfo';
  6.         $totalIsFound = false;
  7.         $freeIsFound = false;
  8.  
  9.         if (file_exists($memInfo)) {
  10.             $handle = @fopen($memInfo, 'r');
  11.  
  12.             if ($handle) {
  13.                 while (!feof($handle)) {
  14.                     $buffer = fgets($handle, 4096);
  15.  
  16.                     if (!$totalIsFound && strpos($buffer, 'MemTotal:') !== false) {
  17.                         sscanf($buffer, 'MemTotal: %d', $total);
  18.                         $totalIsFound = true;
  19.                         continue;
  20.                     }
  21.  
  22.                     if (!$freeIsFound && strpos($buffer, 'MemFree:') !== false) {
  23.                         sscanf($buffer, 'MemFree: %d', $free);
  24.                         $freeIsFound = true;
  25.                     }
  26.  
  27.                     if ($totalIsFound && $freeIsFound) {
  28.                         break;
  29.                     }
  30.                 }
  31.             }
  32.  
  33.             @fclose($handle);
  34.         }
  35.  
  36.         $percent = 100;
  37.  
  38.         if ($total > 0 && $free > 0) {
  39.             $percent = 100 - ((int)$free / ((int)$total / 100));
  40.         }
  41.  
  42.         return [
  43.             'total' => (int)$total * 1024,
  44.             'free' => (int)$free * 1024,
  45.             'usage' => (string)number_format($percent, 2)
  46.         ];
  47.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top