operating-system/php-memory

Wrapper for retrieving information about PHP's memory usage with native PHP functions


Keywords
php, wrapper, memory, usage
License
MIT

Documentation

Wrapper for retrieving information about PHP's memory with native PHP functions

Installation

$ composer require operating-system/php-memory

Usage

use OperatingSystem\Php\Memory\Memory as PhpMemory;
use Unit\Information\Unit;

$phpMemory = new PhpMemory();

$oldPhpMemoryLimit = ini_get('memory_limit');
ini_set('memory_limit', '128M');

// Get the configured PHP memory limit
$phpMemory->getConfiguredLimit()->format(Unit::MEBIBYTE); // '128MiB'
$phpMemory->getConfiguredLimit()->format(Unit::MEGABYTE); // '134.2MB'

// Get memory allocated by PHP
$phpMemory->getAllocated()->format();
$phpMemory->getAllocatedPeak()->format();

// Get memory used by PHP
$phpMemory->getUsed()->format();
$phpMemory->getUsedPeak()->format();

// Get the difference between configured limit and used memory
$phpMemory->getAvailable()->format();

// Get the difference between allocated memory and used memory
$phpMemory->getUnusedButAllocated()->format();

ini_set('memory_limit', $oldPhpMemoryLimit);

For formatting details see https://packagist.org/packages/unit/information.

Testing

$ ./vendor/bin/phpunit

License

The MIT License (MIT). Please see License File for more information.