Created
September 6, 2010 13:30
-
-
Save jeremyFreeAgent/567041 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class faTask extends sfBaseTask | |
{ | |
static private function getMemoryLimit() | |
{ | |
return ini_get('memory_limit')*1024; | |
} | |
static private function getMemoryUsage() | |
{ | |
return round(memory_get_usage(true)/1024,2); | |
} | |
static private function getMemoryUsagePercent() | |
{ | |
return round((self::getMemoryUsage() * 100 / self::getMemoryLimit())); | |
} | |
static private function isGoingToCrashBecauseOfMemory($limit = 60) | |
{ | |
return ($limit < self::getMemoryUsagePercent()); | |
} | |
protected function execute($arguments = array(), $options = array()) | |
{ | |
// Begin the Code | |
foreach ($veryBigDoctrineCollection as $doctrineRecord) | |
{ | |
$doctrineRecord->changeMe(); | |
$doctrineRecord->save(); | |
$doctrineRecord->free(); | |
unset($doctrineRecord); | |
if (self::isGoingToCrashBecauseOfMemory()) | |
{ | |
break; | |
} | |
} | |
// End the Code | |
} | |
} |
LOL :)
You can also start a new php process which consumes a lot of memory, and test the output in the parent, like
http://github.com/rande/sfSolrPlugin/blob/master/lib/task/sfLuceneUpdateModelSystemTask.class.php#L183
you don't have to crash or break, just launch sub process like rande told you, here is my way to do it http://gist.github.com/396110
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Impressive!