From: Vincent Vanwaelscappel Date: Mon, 15 Jan 2024 16:14:06 +0000 (+0100) Subject: wip #6629 @0:20 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=8ee74a3cd0e1564202f4752cd1049cee049ce75e;p=cubist_util.git wip #6629 @0:20 --- diff --git a/src/Math.php b/src/Math.php index 571254f..daf9172 100644 --- a/src/Math.php +++ b/src/Math.php @@ -4,6 +4,22 @@ namespace Cubist\Util; class Math { + public static function toBytes($val) { + $val = trim($val); + $last = strtolower($val[strlen($val)-1]); + $val = substr($val, 0, -1); + switch($last) { + // The 'G' modifier is available since PHP 5.1.0 + case 'g': + $val *= 1024; + case 'm': + $val *= 1024; + case 'k': + $val *= 1024; + } + return $val; + } + public static function interval($val, $min, $max) { if ($min > $max) { diff --git a/src/PHP.php b/src/PHP.php index 43b52f1..7c8f562 100644 --- a/src/PHP.php +++ b/src/PHP.php @@ -22,6 +22,15 @@ class PHP ini_set("memory_limit", $amount); } + public static function isCloseToMemoryLimit($throw = false) + { + $res = (memory_get_usage() / Math::toBytes(ini_get('memory_limit'))) > 0.9; + if ($throw && $res) { + throw new \Exception('Memory is close to the limit'); + } + return $res; + } + /** * @param $file * @return mixed