Skip to content

Commit

Permalink
added memory limit functions
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Dec 26, 2023
1 parent 4ff8d3b commit 75d28a4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/PhPease/Php/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,10 @@ function get_memory_limit() {
}

/**
* get remaining memory in kilobytes
* get remaining memory inbytes
* @return float|int
*/
function get_remaining_memory()
{
$current_memory_usage = memory_get_usage(); // in bytes
$remaining_memory = get_memory_limit() * 1024 - $current_memory_usage; // in bytes

// convert remaining memory from bytes to kilobytes
return $remaining_memory / 1024;
return get_memory_limit() * 1024 - memory_get_usage();
}
18 changes: 18 additions & 0 deletions src/PhPease/Variable/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,22 @@ function var_to_float($var, int $decimals = 2): float
$var = preg_replace('/[^0-9\.]/', '', $var);
$var = number_format((float)$var, $decimals, '.', null);
return floatval($var);
}

/**
* get size of array in bytes
* @param array $array
* @return int
*/
function get_array_size(array $array): int
{
$serializedArray = serialize($array);

if (function_exists('mb_strlen')) {
$sizeInBytes = mb_strlen($serializedArray, '8bit');
} else {
$sizeInBytes = strlen($serializedArray);
}

return $sizeInBytes;
}
9 changes: 9 additions & 0 deletions tests/VariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,13 @@ public function testArrayKeysExists()

$this->assertTrue(array_keys_exists($required, $data));
}

public function testGetArraySize()
{
$a1 = [
'k1' => '123'
];

var_dump( \PhPease\Variable\get_array_size($a1));
}
}

0 comments on commit 75d28a4

Please sign in to comment.