Skip to content

Instantly share code, notes, and snippets.

@jeremyFreeAgent
Created September 6, 2010 13:30
Show Gist options
  • Select an option

  • Save jeremyFreeAgent/567041 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

Impressive!

@jeremyFreeAgent

Copy link
Copy Markdown
Author

LOL :)

@rande

rande commented Sep 6, 2010

Copy link
Copy Markdown

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

pborreli commented Sep 6, 2010

Copy link
Copy Markdown

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