4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\DataCollector;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
18 * MemoryDataCollector.
20 * @author Fabien Potencier <fabien@symfony.com>
22 class MemoryDataCollector extends DataCollector implements LateDataCollectorInterface
24 public function __construct()
28 'memory_limit' => $this->convertToBytes(ini_get('memory_limit')),
35 public function collect(Request $request, Response $response, \Exception $exception = null)
37 $this->updateMemoryUsage();
43 public function lateCollect()
45 $this->updateMemoryUsage();
51 * @return int The memory
53 public function getMemory()
55 return $this->data['memory'];
59 * Gets the PHP memory limit.
61 * @return int The memory limit
63 public function getMemoryLimit()
65 return $this->data['memory_limit'];
69 * Updates the memory usage data.
71 public function updateMemoryUsage()
73 $this->data['memory'] = memory_get_peak_usage(true);
79 public function getName()
84 private function convertToBytes($memoryLimit)
86 if ('-1' === $memoryLimit) {
90 $memoryLimit = strtolower($memoryLimit);
91 $max = strtolower(ltrim($memoryLimit, '+'));
92 if (0 === strpos($max, '0x')) {
93 $max = intval($max, 16);
94 } elseif (0 === strpos($max, '0')) {
95 $max = intval($max, 8);
100 switch (substr($memoryLimit, -1)) {
101 case 't': $max *= 1024;
102 case 'g': $max *= 1024;
103 case 'm': $max *= 1024;
104 case 'k': $max *= 1024;