PHP Classes

File: example/withoutReachingLimit.php

Recommend this page to a friend!
  Classes of nvb   PHP Memory Limit Manager   example/withoutReachingLimit.php   Download  
File: example/withoutReachingLimit.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Memory Limit Manager
Set the PHP memory limit and check if is exceeded
Author: By
Last change:
Date: 4 years ago
Size: 837 bytes
 

Contents

Class file image Download
<?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 * 2));

$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;