Returns the runtime memory consumption for the whole PHP

@param bool $mb (if true the memory consumption is returned in megabytes)
@return float|int
496257
public static function getRealConsumption($mb = true)
    {
        $mem = memory_get_usage(true);
        if ($mb === true) {
            $mem = round($mem/self::MB_DIVIDER, 2);
        }

        return $mem;
    }
414689
public static function getMemoryUsage()
    {
        $memory = false;
        if (function_exists('xdebug_memory_usage')) {
            $memory = xdebug_memory_usage();
        } elseif (function_exists('memory_get_usage')) {
            $memory = memory_get_usage();
        }
        if ($memory === false) {
            return "Memory usage function not found.";
        }
        $usage = number_format(round($memory / 1024 / 1024, 2), 2);
        return "$usage Mb";
    }
64386
public function byteSize()
    {
        return mb_strlen($this->name) +
                mb_strlen($this->type) +
                mb_strlen($this->value);
    }
467396
public static int getMemoryInUse() {
    Runtime runtime = Runtime.getRuntime();
    long mb = 1024 * 1024;
    long total = runtime.totalMemory();
    long free = runtime.freeMemory();
    return (int) ((total - free) / mb);
  }
699977
static public function switchMemoryCapacity($size, $targetUnit = 'KB', $sourceUnit = 'B')
    {
        $supportUnit = [
            'B' => 1,
            'KB' => 2,
            'MB' => 3,
            'GB' => 4,
            'TB' => 5,
        ];
        $targetUnit = strtoupper($targetUnit);
        if (!isset($supportUnit[$targetUnit])) {
            return null;
        }
        if (is_numeric($size)) {
            $size = floatval($size);
            $sourceUnit = strtoupper($sourceUnit);
        } else {
            $size = strtoupper($size);
            if (preg_match('#^(\d*(\.\d*)?)(.*)$#', $size, $m)) {
                $size = $m[1];
                $sourceUnit = $m[3];
                if (!isset($supportUnit[$sourceUnit])) {
                    return "0 {$targetUnit}";
                }
            } else {
                return "0 {$targetUnit}";
            }
        }
        return round($size * pow(1024, $supportUnit[$sourceUnit] - $supportUnit[$targetUnit]), 3) . " {$targetUnit}";
    }
675855
protected function memory()
    {
        $s = $this->grabkey('hw.memsize');
        if (CommonFunctions::executeProgram('vm_stat', '', $pstat, PSI_DEBUG)) {
            // calculate free memory from page sizes (each page = 4096)
            if (preg_match('/^Pages free:\s+(\S+)/m', $pstat, $free_buf)) {
                if (preg_match('/^Anonymous pages:\s+(\S+)/m', $pstat, $anon_buf)
                   && preg_match('/^Pages wired down:\s+(\S+)/m', $pstat, $wire_buf)
                   && preg_match('/^File-backed pages:\s+(\S+)/m', $pstat, $fileb_buf)) {
                        // OS X 10.9 or never
                        $this->sys->setMemFree($free_buf[1] * 4 * 1024);
                        $this->sys->setMemApplication(($anon_buf[1]+$wire_buf[1]) * 4 * 1024);
                        $this->sys->setMemCache($fileb_buf[1] * 4 * 1024);
                        if (preg_match('/^Pages occupied by compressor:\s+(\S+)/m', $pstat, $compr_buf)) {
                            $this->sys->setMemBuffer($compr_buf[1] * 4 * 1024);
                        }
                } else {
                    if (preg_match('/^Pages speculative:\s+(\S+)/m', $pstat, $spec_buf)) {
                        $this->sys->setMemFree(($free_buf[1]+$spec_buf[1]) * 4 * 1024);
                    } else {
                        $this->sys->setMemFree($free_buf[1] * 4 * 1024);
                    }
                    $appMemory = 0;
                    if (preg_match('/^Pages wired down:\s+(\S+)/m', $pstat, $wire_buf)) {
                        $appMemory += $wire_buf[1] * 4 * 1024;
                    }
                    if (preg_match('/^Pages active:\s+(\S+)/m', $pstat, $active_buf)) {
                        $appMemory += $active_buf[1] * 4 * 1024;
                    }
                    $this->sys->setMemApplication($appMemory);

                    if (preg_match('/^Pages inactive:\s+(\S+)/m', $pstat, $inactive_buf)) {
                        $this->sys->setMemCache($inactive_buf[1] * 4 * 1024);
                    }
                }
            } else {
                $lines = preg_split("/\n/", $pstat, -1, PREG_SPLIT_NO_EMPTY);
                $ar_buf = preg_split("/\s+/", $lines[1], 19);
                $this->sys->setMemFree($ar_buf[2] * 4 * 1024);
            }

            $this->sys->setMemTotal($s);
            $this->sys->setMemUsed($this->sys->getMemTotal() - $this->sys->getMemFree());

            if (CommonFunctions::executeProgram('sysctl', 'vm.swapusage | colrm 1 22', $swapBuff, PSI_DEBUG)) {
                $swap1 = preg_split('/M/', $swapBuff);
                $swap2 = preg_split('/=/', $swap1[1]);
                $swap3 = preg_split('/=/', $swap1[2]);
                $dev = new DiskDevice();
                $dev->setName('SWAP');
                $dev->setMountPoint('SWAP');
                $dev->setFsType('swap');
                $dev->setTotal($swap1[0] * 1024 * 1024);
                $dev->setUsed($swap2[1] * 1024 * 1024);
                $dev->setFree($swap3[1] * 1024 * 1024);
                $this->sys->setSwapDevices($dev);
            }
        }
    }
677473
def get_memory_usage():
    
    process = psutil.Process(os.getpid())
    mem = process.memory_info().rss
    return mem / (1024 * 1024)
173756
public function report()
    {
        $result = parent::aggregate();

        $result['memory'] = array_map(function($v){ return $v . 'MB'; }, $result['memory']);

        // report time benchmark
        $lines[] = 'Time: ' . ($result['time'] ?? 0) . ' msec';

        // report memory benchmark
        $lines[] = 'Memory: ';
        $lines[] = ' alloc: ' . $result['memory']['alloc'];
        $lines[] = ' alloc_real: ' . $result['memory']['alloc_real'];
        $lines[] = ' peak: ' . $result['memory']['peak'];
        $lines[] = ' peak_real: ' . $result['memory']['peak_real'];

        foreach($lines as $line){
            $this->logger->debug($line);
        }

        $this->reported = true;
    }
1237705
public function min() {
		if ($this->output !== '') { // min already run
			return $this->output;
		}

		$mbIntEnc = null;
		if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) {
			$mbIntEnc = mb_internal_encoding();
			mb_internal_encoding('8bit');
		}
		$this->input = str_replace("\r\n", "\n", $this->input);
		$this->inputLength = strlen($this->input);

		$this->action(self::ACTION_DELETE_A_B);

		while ($this->a !== null) {
			// determine next command
			$command = self::ACTION_KEEP_A; // default
			if ($this->a === ' ') {
				if (($this->lastByteOut === '+' || $this->lastByteOut === '-')
				    && ($this->b === $this->lastByteOut)
				) {
					// Don't delete this space. If we do, the addition/subtraction
					// could be parsed as a post-increment
				} elseif (!$this->isAlphaNum($this->b)) {
					$command = self::ACTION_DELETE_A;
				}
			} elseif ($this->a === "\n") {
				if ($this->b === ' ') {
					$command = self::ACTION_DELETE_A_B;
					// in case of mbstring.func_overload & 2, must check for null b,
					// otherwise mb_strpos will give WARNING
				} elseif ($this->b === null
				          || (false === strpos('{[(+-', $this->b)
				              && !$this->isAlphaNum($this->b))
				) {
					$command = self::ACTION_DELETE_A;
				}
			} elseif (!$this->isAlphaNum($this->a)) {
				if ($this->b === ' '
				    || ($this->b === "\n"
				        && (false === strpos('}])+-"\'', $this->a)))
				) {
					$command = self::ACTION_DELETE_A_B;
				}
			}
			$this->action($command);
		}
		$this->output = trim($this->output);

		if ($mbIntEnc !== null) {
			mb_internal_encoding($mbIntEnc);
		}
		return $this->output;
	}
726295
public static function mem_check($need)
    {
        $mem_limit = parse_bytes(ini_get('memory_limit'));
        $memory    = function_exists('memory_get_usage') ? memory_get_usage() : 16*1024*1024; // safe value: 16MB

        return $mem_limit > 0 && $memory + $need > $mem_limit ? false : true;
    }
782615
private function _memory()
    {
        $mems = "";
        $tswap = "";
        $pswap = "";
        foreach ($this->readaixdata() as $line) {
            if (preg_match("/^Good Memory Size:\s+(\d+)\s+MB/", $line, $ar_buf)) {
                $mems = $ar_buf[1];
            }
            if (preg_match("/^\s*Total Paging Space:\s+(\d+)MB/", $line, $ar_buf)) {
                $tswap = $ar_buf[1];
            }
            if (preg_match("/^\s*Percent Used:\s+(\d+)%/", $line, $ar_buf)) {
                $pswap = $ar_buf[1];
            }
        }
        if (trim($mems) != "") {
            $mems = $mems*1024*1024;
            $this->sys->setMemTotal($mems);
            $memu = 0;
            $memf = 0;
            if (CommonFunctions::executeProgram('svmon', '-G', $buf)) {
                if (preg_match("/^memory\s+\d+\s+(\d+)\s+/", $buf, $ar_buf)) {
                    $memu = $ar_buf[1]*1024*4;
                    $memf = $mems - $memu;
                }
            }
            $this->sys->setMemUsed($memu);
            $this->sys->setMemFree($memf);
//            $this->sys->setMemApplication($mems);
//            $this->sys->setMemBuffer($mems);
//            $this->sys->setMemCache($mems);
        }
        if (trim($tswap) != "") {
            $dev = new DiskDevice();
            $dev->setName("SWAP");
            $dev->setFsType('swap');
            $dev->setTotal($tswap * 1024 * 1024);
            if (trim($pswap) != "") {
                $dev->setUsed($dev->getTotal() * $pswap / 100);
            }
            $dev->setFree($dev->getTotal() - $dev->getUsed());
            $this->sys->setSwapDevices($dev);
        }
    }
96607
public static int getTotalPhysicalMemorySizeInMB() {

    int memorySizeInMB;
    try {
      long memorySizeInBytes = ((com.sun.management.OperatingSystemMXBean) ManagementFactory
                    .getOperatingSystemMXBean()).getTotalPhysicalMemorySize();

      memorySizeInMB = (int) (memorySizeInBytes / BYTES_IN_MEGABYTE);
    } catch (Exception e) {
      memorySizeInMB = -1;
    }

    return memorySizeInMB;
  }
1151392
private function getMemoryUsage(int $width = 0, int $height = 0, $multiply_by = 1)
    {

        if (!$width) {
            $width = $this->image_width;
        }

        if (!$height) {
            $height = $this->image_height;
        }

        $depth_kilobytes = 65536; // 64 KB

        return ($width * $height * 4 + $depth_kilobytes) * self::FUDGE * $multiply_by;
    }
502963
public function getQueryLength($mb = true)
    {
        return ($mb) ? mb_strlen($this->query) : strlen($this->query);
    }
1227404
public function returnFullData() {

        $aMemory = $this->getSNMP()->realWalk1d(self::OID_MEMORY);

        $aReturn = Array();
        $aReturn['total_swap_size'] = $aMemory[self::OID_TOTAL_SWAP_SIZE][0];
        $aReturn['avaliable_swap_size'] = $aMemory[self::OID_AVALIABLE_SWAP_SIZE][0];
        $aReturn['total_ram_machine'] = $aMemory[self::OID_TOTAL_RAM_IN_MACHINE][0];
        $aReturn['avaliable_ram_real'] = $aMemory[self::OID_AVALIABLE_RAM_REAL][0];
        $aReturn['total_memory'] = $aMemory[self::OID_TOTAL_MEMORY][0];
        $aReturn['total_ram_buffered'] = $aMemory[self::OID_TOTAL_RAM_BUFFERED][0];
        $aReturn['total_cached_memory'] = $aMemory[self::OID_TOTAL_CACHED_MEMORY][0];
        $aReturn['total_memory_used'] = $aReturn['total_ram_machine'] - ($aReturn['avaliable_ram_real'] + $aReturn['total_cached_memory'] + $aReturn['total_ram_buffered']);
        $aReturn['total_swap_used'] = $aReturn['total_swap_size'] - $aReturn['avaliable_swap_size'];
        $aReturn['perc_memory_used'] = round((($aReturn['total_memory_used'] * 100) / $aReturn['total_ram_machine']), 2);
        
        if ($aReturn['avaliable_swap_size'] < $aReturn['total_swap_size']) {
            $aReturn['perc_swap_used'] = round((($aReturn['total_swap_used'] * 100) / $aReturn['total_swap_size']), 2);
        } else {
            $aReturn['perc_swap_used'] = 0;
        }

        return $aReturn;
    }
295301
private static function calculateMeanMicroAmount($min, $max)
    {
        if ($min === null || $max === null) {
            return null;
        }
        if ($min->getMicroAmount() === null
            || $max->getMicroAmount() === null) {
            return null;
        }

        return ($min->getMicroAmount() + $max->getMicroAmount()) / 2;
    }
1081146
protected function get_memory_limit() {
		if ( function_exists( 'ini_get' ) ) {
			$memory_limit = ini_get( 'memory_limit' );
		} else {
			$memory_limit = '256M';
		}

		if ( ! $memory_limit || - 1 == $memory_limit ) {
			// Unlimited, set to 1GB
			$memory_limit = '1000M';
		}

		return intval( $memory_limit ) * 1024 * 1024;
	}