Skip to content

Instantly share code, notes, and snippets.

@jeremyFreeAgent
Created September 6, 2010 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyFreeAgent/567041 to your computer and use it in GitHub Desktop.
Save jeremyFreeAgent/567041 to your computer and use it in GitHub Desktop.
<?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
}
}
@clemherreman
Copy link

Impressive!

@jeremyFreeAgent
Copy link
Author

LOL :)

@rande
Copy link

rande commented Sep 6, 2010

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

@pborreli
Copy link

pborreli commented Sep 6, 2010

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