/** * Returns the amount of memory available to PHP, minus the amount * currently being used, in bytes. * * @param boolean $realUsage * @param boolean $forceLimitCheck * @return int */ public static function getAvailableMemory($realUsage = false, $forceLimitCheck = false) { if (!self::$_memLimit || $forceLimitCheck) { $iniVal = ini_get('memory_limit'); if ($iniVal == '-1') { /* Default to a gig, though this does mean that this method could return a negative number. */ self::$_memLimit = pow(1024, 3); } elseif (ctype_digit($iniVal)) { self::$_memLimit = (int) $iniVal; } else { $lastChar = strtolower(substr($iniVal, -1)); $intPortion = (int) substr($iniVal, 0, strlen($iniVal) - 1); if ($lastChar == 'k') { $multiplier = 1024; } elseif ($lastChar == 'm') { $multiplier = pow(1024, 2); } elseif ($lastChar == 'g') { $multiplier = pow(1024, 3); } else { // Shouldn't happen return false; } self::$_memLimit = $intPortion * $multiplier; } } return self::$_memLimit - memory_get_usage($realUsage); }