Completed
Branch master (d17104)
by Christian
21:20
created

MemoryUtility   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 19
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A isMemoryConsumptionTooHigh() 0 7 3
1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Adminpanel\Utility;
5
6
/*
7
 * This file is part of the TYPO3 CMS project.
8
 *
9
 * It is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License, either version 2
11
 * of the License, or any later version.
12
 *
13
 * For the full copyright and license information, please read the
14
 * LICENSE.txt file that was distributed with this source code.
15
 *
16
 * The TYPO3 project - inspiring people to share!
17
 */
18
19
use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21
/**
22
 * Helper class to keep track of memory consumption in loggers to reduce risk of running out of memory
23
 *
24
 * @internal
25
 */
26
class MemoryUtility
27
{
28
    private const MINIMAL_PERCENT_OF_FREE_MEMORY = 0.30;
29
30
    /**
31
     * Checks memory usage used in current process - this is no guarantee for not running out of memory
32
     * but should prevent memory exhaustion due to the admin panel in most cases.
33
     * The loggers will stop logging once the amount of free memory falls below the threshold (see
34
     * MINIMAL_PERCENT_OF_FREE_MEMORY const).
35
     *
36
     * @return bool
37
     */
38
    public static function isMemoryConsumptionTooHigh(): bool
39
    {
40
        $iniLimit = ini_get('memory_limit');
41
        $memoryLimit = $iniLimit === '-1' ? -1 : GeneralUtility::getBytesFromSizeMeasurement($iniLimit);
42
        $freeMemory = $memoryLimit - memory_get_usage(true);
43
44
        return $memoryLimit > 0 && $freeMemory < (self::MINIMAL_PERCENT_OF_FREE_MEMORY * $memoryLimit);
45
    }
46
}
47