You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MemoryInfo.php 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018, Michael Weimann (<mail@michael-weimann.eu>)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OC;
  23. /**
  24. * Helper class that covers memory info.
  25. */
  26. class MemoryInfo {
  27. const RECOMMENDED_MEMORY_LIMIT = 512 * 1024 * 1024;
  28. /**
  29. * Tests if the memory limit is greater or equal the recommended value.
  30. *
  31. * @return bool
  32. */
  33. public function isMemoryLimitSufficient(): bool {
  34. $memoryLimit = $this->getMemoryLimit();
  35. return $memoryLimit === -1 || $memoryLimit >= self::RECOMMENDED_MEMORY_LIMIT;
  36. }
  37. /**
  38. * Returns the php memory limit.
  39. *
  40. * @return int The memory limit in bytes.
  41. */
  42. public function getMemoryLimit(): int {
  43. $iniValue = trim(ini_get('memory_limit'));
  44. if ($iniValue === '-1') {
  45. return -1;
  46. } else if (is_numeric($iniValue) === true) {
  47. return (int)$iniValue;
  48. } else {
  49. return $this->memoryLimitToBytes($iniValue);
  50. }
  51. }
  52. /**
  53. * Converts the ini memory limit to bytes.
  54. *
  55. * @param string $memoryLimit The "memory_limit" ini value
  56. * @return int
  57. */
  58. private function memoryLimitToBytes(string $memoryLimit): int {
  59. $last = strtolower(substr($memoryLimit, -1));
  60. $memoryLimit = (int)substr($memoryLimit, 0, -1);
  61. // intended fall trough
  62. switch($last) {
  63. case 'g':
  64. $memoryLimit *= 1024;
  65. case 'm':
  66. $memoryLimit *= 1024;
  67. case 'k':
  68. $memoryLimit *= 1024;
  69. }
  70. return $memoryLimit;
  71. }
  72. }