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()
32 public function collect(Request $request, Response $response, \Exception $exception = null)
34 $this->updateMemoryUsage();
40 public function reset()
44 'memory_limit' => $this->convertToBytes(ini_get('memory_limit')),
51 public function lateCollect()
53 $this->updateMemoryUsage();
59 * @return int The memory
61 public function getMemory()
63 return $this->data['memory'];
67 * Gets the PHP memory limit.
69 * @return int The memory limit
71 public function getMemoryLimit()
73 return $this->data['memory_limit'];
77 * Updates the memory usage data.
79 public function updateMemoryUsage()
81 $this->data['memory'] = memory_get_peak_usage(true);
87 public function getName()
92 private function convertToBytes($memoryLimit)
94 if ('-1' === $memoryLimit) {
98 $memoryLimit = strtolower($memoryLimit);
99 $max = strtolower(ltrim($memoryLimit, '+'));
100 if (0 === strpos($max, '0x')) {
101 $max = \intval($max, 16);
102 } elseif (0 === strpos($max, '0')) {
103 $max = \intval($max, 8);
108 switch (substr($memoryLimit, -1)) {
109 case 't': $max *= 1024;
111 case 'g': $max *= 1024;
113 case 'm': $max *= 1024;
115 case 'k': $max *= 1024;