Skip to content

Commit

Permalink
Fixed memory issue
Browse files Browse the repository at this point in the history
  • Loading branch information
uuur86 committed May 15, 2021
1 parent 0c23b24 commit a11f529
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/StringObjects.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @package strobj
* @license GPLv2
* @author Uğur Biçer <[email protected]>
* @version 0.5.0
* @version 0.5.1
*/

namespace StrObj;
Expand Down Expand Up @@ -37,7 +37,7 @@ public function __construct($obj, int $memory)
{
$this->obj = $obj;

self::_setmemory($memory);
self::_setMemoryLimit($memory);
}


Expand All @@ -59,21 +59,40 @@ public static function instance($obj, int $memory = 50)



private static function _setmemory(int $mem)
public static function convertToByte($amount)
{
$value = intval($amount);
$unit = strtolower(substr($amount, strlen($value)));

if ($unit == "g" || $unit == "gb") {
$value *= 1024 * 1024 * 1024;
} else if ($unit == "m" || $unit == "mb") {
$value *= 1024 * 1024;
} else if ($unit == "k" || $unit == "kb") {
$value *= 1024;
}

return $value;
}



private static function _setMemoryLimit(int $mem)
{
// Its check only once for performance.
if (self::$memory_limit > 0) {
return;
}

$toMb = 1024 * 1024;
$default = 50 * $toMb;
$mem *= $toMb;
$ini_get_mem = ini_get('memory_limit') ? ini_get('memory_limit') : 0;
$ini_get_mem = intval($ini_get_mem) * $toMb;
$mbToByte = 1024 * 1024;
$default = 50 * $mbToByte;

$mem *= $mbToByte;

$ini_get_mem = ini_get('memory_limit') ? self::convertToByte(ini_get('memory_limit')) : 0;

if (empty($ini_get_mem)) {
$mem = $default * $toMb;
$mem = $default;
} else if ($mem > $ini_get_mem) {
$mem = $ini_get_mem;
}
Expand Down

0 comments on commit a11f529

Please sign in to comment.