API CHANGE: Added increase_memory_limit_to() for increasing but not decreasing memory limit.

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@78632 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2009-06-08 07:16:25 +00:00
parent f74069f060
commit 8aea22c283
3 changed files with 66 additions and 20 deletions

View File

@ -125,26 +125,8 @@ define('PR_LOW',10);
/**
* Ensure we have enough memory
*/
$memString = ini_get("memory_limit");
switch(strtolower(substr($memString, -1))) {
case "k":
$memory = round(substr($memString, 0, -1)*1024);
break;
case "m":
$memory = round(substr($memString, 0, -1)*1024*1024);
break;
case "g":
$memory = round(substr($memString, 0, -1)*1024*1024*1024);
break;
default:
$memory = round($memString);
}
// Check we have at least 64M
if ($memory < (64 * 1024 * 1024)) {
// Increase memory limit
ini_set('memory_limit', '64M');
}
increase_memory_limit_to('64M');
///////////////////////////////////////////////////////////////////////////////
// INCLUDES
@ -298,4 +280,28 @@ function _t($entity, $string = "", $priority = 40, $context = "") {
return i18n::_t($entity, $string, $priority, $context);
}
/**
* Increase the memory limit to the given level if it's currently too low.
* @param A memory limit string, such as "64M"
*/
function increase_memory_limit_to($memoryLimit) {
// Increase the memory limit if it's too low
if(translate_memstring($memoryLimit) > translate_memstring(ini_get('memory_limit'))) {
ini_set('memory_limit', $memoryLimit);
}
}
/**
* Turn a memory string, such as 512M into an actual number of bytes.
* @param A memory limit string, such as "64M"
*/
function translate_memstring($memString) {
switch(strtolower(substr($memString, -1))) {
case "k": return round(substr($memString, 0, -1)*1024);
case "m": return round(substr($memString, 0, -1)*1024*1024);
case "g": return round(substr($memString, 0, -1)*1024*1024*1024);
default: return round($memString);
}
}
?>

View File

@ -130,7 +130,9 @@ abstract class BulkLoader extends ViewableData {
*/
public function load($filepath, $memory_limit='512M') {
ini_set('max_execution_time', 3600);
ini_set('memory_limit', $memory_limit);
increase_memory_limit_to($memory_limit);
return $this->processAll($filepath);
}

38
tests/MemoryLimitTest.php Normal file
View File

@ -0,0 +1,38 @@
<?php
class MemoryLimitTest extends SapphireTest {
function testIncreaseMemoryLimitTo() {
ini_set('memory_limit', '64M');
// It can go up
increase_memory_limit_to('128M');
$this->assertEquals('128M', ini_get('memory_limit'));
// But not down
increase_memory_limit_to('64M');
$this->assertEquals('128M', ini_get('memory_limit'));
// Test the different kinds of syntaxes
increase_memory_limit_to(1024*1024*200);
$this->assertEquals(1024*1024*200, ini_get('memory_limit'));
increase_memory_limit_to('409600K');
$this->assertEquals('409600K', ini_get('memory_limit'));
increase_memory_limit_to('1G');
$this->assertEquals('1G', ini_get('memory_limit'));
}
///////////////////
private $origLimit;
function setUp() {
$this->origLimit = ini_get('memory_limit');
}
function tearDown() {
ini_set('memory_limit', $this->origLimit);
}
}