I have a php script running as a service on ubuntu, it's an infinite loop that does external checks to save to the mysql database.
It consumes up to 1GB of memory, checking using the HTOP command
Running the get_memory_usage commands with each iteration of while, I have different values, when I don't pass the true parameter to get the Real usage, the value remains stable, however with the true parameter, it grows with each iteration.
Mem: 3.49 mb - 132 mb 02Mem: 1.01 mb - 132 mb 03Mem: 4.66 mb - 146 mb 02Mem: 1.01 mb - 146 mb 03Mem: 5.01 mb - 148 mb 02Mem: 1.01 mb - 148 mb 03Mem: 6.12 mb - 148 mb 02Mem: 1.01 mb - 148 mb 03Mem: 6.4 mb - 162 mb 02Mem: 1.13 mb - 162 mb 03
I have already separated the execution block into a single function outside of while, and called gc_collect_cycles(), but it didn't solve the problem.
$loop = Array();gc_enable();function execute(&$loop, $rep_server){ foreach($rep_server->objetos as $server){ $dates = calcDateGetData("","",0,false,false,2,40); $loop[$sid]['data_inicial'] = $dates[0]; $params['dataCadastroInicial'] = $loop[$sid]['data_inicial']; $js_ar = $server->sendRequest(REQUEST::ObterClientesServicos,$params); if(isset($js_ar["dados"])){ if(count($js_ar["dados"]) > 0){ foreach ($js_ar["dados"] as $key2) { $cliente = new ClienteServico(); $cliente->uid_ = DB::toUid_($server->id, $key2['id_servico']); $remoto[] = $cliente; } } $repOs->objetos = $remoto; $repOs->updateAllChanges($repAll); } } ... ... echo "Mem: ".convertBytes(memory_get_usage())." - ".convertBytes(memory_get_usage(true))." 02 \n";}while(true){ echo "Mem: ".convertBytes(memory_get_usage())." - ".convertBytes(memory_get_usage(true))." 01 \n"; execute($loop, $rep_server); gc_collect_cycles(); DB::getInstancia()->close(); echo "Mem: ".convertBytes(memory_get_usage())." - ".convertBytes(memory_get_usage(true))." 03 \n"; $c = count($opts); if($c > 1 && $opts[$c-2] == "-d" && is_numeric($opts[$c-1])){ echo PHP_EOL." SLEEP FOR ".$opts[$c-1]."(s)".PHP_EOL; sleep($opts[$c-1]); } else return; echo "\n";}
I don't think there's a memory leak, but that the memory freed by PHP is not being freed in Ubuntu, any ideas?