]> _ Git - cubist_util.git/commitdiff
wip #6629 @0:20
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 15 Jan 2024 16:14:06 +0000 (17:14 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 15 Jan 2024 16:14:06 +0000 (17:14 +0100)
src/Math.php
src/PHP.php

index 571254f50d8ca3202b5773cd060c61dde61876b4..daf91728bd7cc479ff87fd9adf58f4ce23814c08 100644 (file)
@@ -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) {
index 43b52f154dd0d25c7ca6cf9c4e6f0b6d833b2a72..7c8f562210586690fde978703451166b9171539c 100644 (file)
@@ -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