From 8ee74a3cd0e1564202f4752cd1049cee049ce75e Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 15 Jan 2024 17:14:06 +0100 Subject: [PATCH] wip #6629 @0:20 --- src/Math.php | 16 ++++++++++++++++ src/PHP.php | 9 +++++++++ 2 files changed, 25 insertions(+) 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 -- 2.39.5