Skip to content

Instantly share code, notes, and snippets.

@thbighead
Created May 13, 2019 18:24
Show Gist options
  • Save thbighead/b86e0170c192b78d34384fd9e0595fe9 to your computer and use it in GitHub Desktop.
Save thbighead/b86e0170c192b78d34384fd9e0595fe9 to your computer and use it in GitHub Desktop.
Using return_bytes, this helper runs the Garbage Collector when the memory usage passes a given percentage
/**
* @param float $percentage A value between 0 and 1
*/
function cleanMemoryWithGC($percentage)
{
if ((return_bytes(ini_get('memory_limit')) * $percentage) <= memory_get_usage()) {
$percentage *= 100;
Log::info('cleanMemoryWithGC', ['message' => "Rodando o Garbage Collector, pois mais de $percentage% do limite de memória disponível para o PHP está sendo utilizado..."]);
gc_collect_cycles();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment