Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Data Fields | Protected Member Functions | Static Protected Member Functions
Memory Class Reference

Public Member Functions

 __construct (\Magento\Framework\Shell $shell)
 
 getRealMemoryUsage ()
 

Static Public Member Functions

static convertToBytes ($number)
 
static isMacOs ()
 

Data Fields

const MEMORY_UNITS = 'BKMGTPE'
 

Protected Member Functions

 _getUnixProcessMemoryUsage ($pid)
 
 _getWinProcessMemoryUsage ($pid)
 

Static Protected Member Functions

static _convertToNumber ($number)
 

Detailed Description

Definition at line 13 of file Memory.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Shell  $shell)

Inject dependencies

Parameters
\Magento\Framework\Shell$shell

Definition at line 33 of file Memory.php.

34  {
35  $this->_shell = $shell;
36  }
if(!file_exists($installConfigFile)) if(!defined('TESTS_INSTALLATION_DB_CONFIG_FILE')) $shell
Definition: bootstrap.php:46

Member Function Documentation

◆ _convertToNumber()

static _convertToNumber (   $number)
staticprotected

Remove non-numeric characters in the string to cast it to a numeric value

Incoming number can be presented in arbitrary format that depends on locale. We don't possess locale information. So the best can be done is to treat number as an integer and eliminate delimiters. Method will not behave correctly with non-integer numbers for the following reason:

  • if value has more than one delimiter, such as in French notation: "1 234,56" – then we can infer decimal part
  • but the value has only one delimiter, such as "234,56", then it is impossible to know whether it is decimal separator or not. Only knowing the right format would allow this.
Parameters
$number
Returns
string
Exceptions

Definition at line 139 of file Memory.php.

140  {
141  preg_match_all('/(\D+)/', $number, $matches);
142  if (count(array_unique($matches[0])) > 1) {
143  throw new \InvalidArgumentException(
144  "The number '{$number}' seems to have decimal part. Only integer numbers are supported."
145  );
146  }
147  return preg_replace('/\D+/', '', $number);
148  }
$number
Definition: details.phtml:22

◆ _getUnixProcessMemoryUsage()

_getUnixProcessMemoryUsage (   $pid)
protected

Retrieve the current process' memory usage using Unix command line interface

int $pid int Memory usage in bytes

Definition at line 67 of file Memory.php.

68  {
69  // RSS - resident set size, the non-swapped physical memory
70  $command = 'ps --pid %s --format rss --no-headers';
71  if ($this->isMacOS()) {
72  $command = 'ps -p %s -o rss=';
73  }
74  $output = $this->_shell->execute($command, [$pid]);
75  $result = $output . 'k';
76  // kilobytes
78  }

◆ _getWinProcessMemoryUsage()

_getWinProcessMemoryUsage (   $pid)
protected

Retrieve the current process' memory usage using Windows command line interface

int $pid int Memory usage in bytes

Definition at line 87 of file Memory.php.

88  {
89  $output = $this->_shell->execute('tasklist.exe /fi %s /fo CSV /nh', ["PID eq {$pid}"]);
90 
91  $arr = str_getcsv($output);
92  $memory = $arr[4];
93  return self::convertToBytes($memory);
94  }

◆ convertToBytes()

static convertToBytes (   $number)
static

Convert a number optionally followed by the unit symbol (B, K, M, G, etc.) to bytes

Parameters
string$numberString representation of a number
Returns
int
Exceptions

Definition at line 104 of file Memory.php.

105  {
106  if (!preg_match('/^(.*\d)\h*(\D)$/', $number, $matches)) {
107  throw new \InvalidArgumentException("Number format '{$number}' is not recognized.");
108  }
109  $unitSymbol = strtoupper($matches[2]);
110  if (false === strpos(self::MEMORY_UNITS, $unitSymbol)) {
111  throw new \InvalidArgumentException("The number '{$number}' has an unrecognized unit: '{$unitSymbol}'.");
112  }
113  $result = self::_convertToNumber($matches[1]);
114  $pow = $unitSymbol ? strpos(self::MEMORY_UNITS, $unitSymbol) : 0;
115  $is32Bit = PHP_INT_SIZE == 4;
116  if ($is32Bit && $pow >= 4) {
117  throw new \OutOfBoundsException("A 32-bit system is unable to process such a number.");
118  }
119  if ($unitSymbol) {
120  $result *= pow(1024, $pow);
121  }
122  return (int)$result;
123  }
$number
Definition: details.phtml:22
static _convertToNumber($number)
Definition: Memory.php:139

◆ getRealMemoryUsage()

getRealMemoryUsage ( )

Retrieve the effective memory usage of the current process

memory_get_usage() cannot be used because of the bug int Memory usage in bytes

Definition at line 46 of file Memory.php.

47  {
48  $pid = getmypid();
49  try {
50  // try to use the Windows command line
51  // some ports of Unix commands on Windows, such as MinGW, have limited capabilities and cannot be used
52  $result = $this->_getWinProcessMemoryUsage($pid);
53  } catch (\Magento\Framework\Exception\LocalizedException $e) {
54  // fall back to the Unix command line
55  $result = $this->_getUnixProcessMemoryUsage($pid);
56  }
57  return $result;
58  }

◆ isMacOs()

static isMacOs ( )
static

Whether the operating system belongs to the Mac family

boolean

Definition at line 156 of file Memory.php.

157  {
158  return strtoupper(PHP_OS) === 'DARWIN';
159  }

Field Documentation

◆ MEMORY_UNITS

const MEMORY_UNITS = 'BKMGTPE'

Prefixes to specify unit of measure for memory amount

Warning: it is important to maintain the exact order of letters in this literal, as it is used to convert string with units to bytes

Definition at line 21 of file Memory.php.


The documentation for this class was generated from the following file: