PHP Classes

PHP Memory Limit Manager: Set the PHP memory limit and check if is exceeded

Recommend this page to a friend!
  Info   View files Example   View files View files (41)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 117 This week: 1All time: 9,529 This week: 560Up
Version License PHP version Categories
php_component_memory 1.0GNU Lesser Genera...5PHP 5, System information, Hardware
Description 

Author

This package can be used to set the PHP memory limit and check if is exceeded.

It can dynamically set the current PHP script memory usage limit by taking a limit value as parameter in KB, MB and GB.

The package can also set the size of limit buffer, so it can check if the current script memory usage tell the current application that it has reached a given limit that is the current hard memory limit minus that buffer size.

Innovation Award
PHP Programming Innovation award nominee
February 2020
Number 5
PHP has a setting that limits the usage of memory by the current script to avoid that the script uses too much memory and makes the current system unusable.

Applications can also check if the current memory usage is near the configured limit.

This package takes advantage of that possibility to allow applications to execute certain actions or avoid other actions, so the current script does not exceed the current memory limit and terminates abnormally.

Manuel Lemos
Picture of nvb
  Performance   Level  
Name: nvb <contact>
Classes: 20 packages by
Country: Germany Germany
Age: ???
All time rank: 150195 in Germany Germany
Week rank: 109 Up6 in Germany Germany Up
Innovation award
Innovation award
Nominee: 12x

Winner: 1x

Example

<?php
/**
 * @author stev leibelt <artodeto@bazzline.net>
 * @since 2014-07-27
 */

require_once __DIR__ . '/../vendor/autoload.php';

echo
'starting example' . PHP_EOL;

$manager = new Net\Bazzline\Component\MemoryLimitManager\MemoryLimitManager();

$currentMemoryUsageInBytes = memory_get_usage(true);

$manager->setBufferInMegaBytes(0);
$manager->setLimitInBytes($currentMemoryUsageInBytes);

$data = array();

for (
$iterator = 0; $iterator < 10; ++$iterator) {
   
$data[$iterator] = true;
    if (
$manager->isLimitReached()) {
        echo
'error - memory limit of ' . $manager->getLimitInBytes() . ' bytes reached (current usage: ' . $manager->getCurrentUsageInBytes() . ' bytes)' . PHP_EOL;
        exit(
1);
    }
}

echo
'finished example with memory usage of ' . $manager->getCurrentUsageInMegaBytes() . ' mb' . PHP_EOL;


Details

Memory Limit Manager Component for PHP

This free as in freedom component helps you to validate if your script reaches the allowed maximum memory limit.

Furthermore, you can set your own memory limit (as long as it is below the limit in you php.ini).

The build status of the current master branch is tracked by Travis CI: build status Latest stable

The scrutinizer status are: code quality

The versioneye status is: dependencies

Downloads: Downloads this Month

It is also available at openhub.net.

Benefits

  • provides easy setting of memory limit
  • gives you the advantage to add a buffer before reaching the limit to easy up reacting when limit is reached
  • helps you to set the limit in bytes, kilo bytes, mega bytes or giga bytes (same for the buffer)
  • comes with DependentInterface and AwareInterface

Examples

Install

By Hand

mkdir -p vendor/net_bazzline/php_component_memory_limit_manager
cd vendor/net_bazzline/php_component_memory_limit_manager
git clone https://github.com/bazzline/php_component_memory_limit_manager

With Packagist

composer require net_bazzline/php_component_memory_limit_manager:dev-master

Usage

$manager = new Net\Bazzline\Component\MemoryLimitManager\MemoryLimitManager();
$manager->setBufferInMegaBytes(4);
$manager->setLimitInMegaBytes(64);

while (!empty($dataSet)) {
    if ($manager->isLimitReached()) {
        //exit while loop, shutdown process
    } else {
        $data = array_shift($dataSet);
        //work on data set
    }
}

API

Thanks to apigen, the api is available in the document section or online.

History

  • upcomming * @todo * implement way of measure/calculate the amount of memory for next iteration
  • 1.1.0 - released at 27.02.2016 * added dedicated travis integration test for php 7.0 * moved to psr-4 autoloading * removed code coverage * removed dead link * removed dependency to phpmd * updated dependencies
  • 1.0.9 - released at 11.12.2015 * updated dependencies
  • 1.0.8 - released at 18.11.2015 * updated dependencies
  • 1.0.7 - released at 27.08.2015 * updated dependencies
  • 1.0.6 - released at 04.07.2015 * updated dependencies
  • 1.0.5 - released at 22.05.2015 * updated dependencies
  • 1.0.4 - released at 02.08.2015 * updated dependencies * removed dependency to apigen
  • 1.0.3 - released at 31.08.2014 * fixed broken composer.json
  • 1.0.2 - released at 31.08.2014 * added versioneye * added scrutinizer * fixed small bugs * updated dependencies
  • 1.0.1 - released at 27.07.2014 * added missing DependentInterface and AwareInterface
  • 1.0.0 - released at 27.07.2014 * initial commit with examples, unit tests and api documentation

Future Improvements

  • if you have one, create a feature request, fork it (and push it back :-))

  Files folder image Files  
File Role Description
Files folder imagedocument (15 files, 1 directory)
Files folder imageexample (2 files)
Files folder imagesource (4 files)
Files folder imagetest (2 files)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file generate_api Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  document  
File Role Description
Files folder imageresources (11 files)
  Accessible without login HTML file 404.html Doc. Documentation
  Accessible without login HTML file class-Net.Bazzline...umentException.html Doc. Documentation
  Accessible without login HTML file class-Net.Bazzline...ryLimitManager.html Doc. Documentation
  Accessible without login HTML file class-Net.Bazzline...AwareInterface.html Doc. Documentation
  Accessible without login HTML file class-Net.Bazzline...ndentInterface.html Doc. Documentation
  Accessible without login Plain text file elementlist.js Data Auxiliary data
  Accessible without login HTML file index.html Doc. Documentation
  Accessible without login HTML file namespace-Net.Bazzline.Component.html Doc. Documentation
  Accessible without login HTML file namespace-Net.Bazz...ryLimitManager.html Doc. Documentation
  Accessible without login HTML file namespace-Net.Bazzline.html Doc. Documentation
  Accessible without login HTML file namespace-Net.html Doc. Documentation
  Accessible without login HTML file source-class-Net.B...umentException.html Doc. Documentation
  Accessible without login HTML file source-class-Net.B...ryLimitManager.html Doc. Documentation
  Accessible without login HTML file source-class-Net.B...AwareInterface.html Doc. Documentation
  Accessible without login HTML file source-class-Net.B...ndentInterface.html Doc. Documentation

  Files folder image Files  /  document  /  resources  
File Role Description
  Accessible without login Image file collapsed.png Icon Icon image
  Accessible without login Plain text file combined.js Data Auxiliary data
  Accessible without login Image file footer.png Data Auxiliary data
  Accessible without login Image file inherit.png Icon Icon image
  Accessible without login Image file resize.png Icon Icon image
  Accessible without login Image file sort.png Icon Icon image
  Accessible without login Plain text file style.css Data Auxiliary data
  Accessible without login Image file tree-cleaner.png Icon Icon image
  Accessible without login Image file tree-hasnext.png Icon Icon image
  Accessible without login Image file tree-last.png Data Auxiliary data
  Accessible without login Image file tree-vertical.png Icon Icon image

  Files folder image Files  /  example  
File Role Description
  Accessible without login Plain text file withoutReachingLimit.php Example Example script
  Accessible without login Plain text file withReachingLimit.php Example Example script

  Files folder image Files  /  source  
File Role Description
  Plain text file InvalidArgumentException.php Class Class source
  Plain text file MemoryLimitManager.php Class Class source
  Plain text file MemoryLimitManagerAwareInterface.php Class Class source
  Plain text file MemoryLimitManagerDependentInterface.php Class Class source

  Files folder image Files  /  test  
File Role Description
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file MemoryLimitManagerTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:117
This week:1
All time:9,529
This week:560Up