@Jony1337

Почему не работает gc_collect_cycles() в php?

Привет , быль проблемы с памятью в php , нашел в гугле gc_collect_cycles() , использовал в коде о всё равно после 1 итераций выдает 500 Internal Server Error.
ini_set('max_execution_time', 0);
  ini_set("memory_limit","512M");

  $before=0;
$before = memory_get_usage();
 $start = microtime(true);
function multiRequest($data, $options = array()) {
 gc_enable();
  // array of curl handles
  $curly = array();
  // data to be returned
  $result = array();
 
  // multi handle
  $mh = curl_multi_init();
 
  // loop through $data and create curl handles
  // then add them to the multi-handle
  foreach ($data as $id => $d) {
 
    $curly[$id] = curl_init();
 
    $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
    curl_setopt($curly[$id], CURLOPT_URL,            $url);
    curl_setopt($curly[$id], CURLOPT_HEADER,         0);
    curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
 
    // post?
    if (is_array($d)) {
      if (!empty($d['post'])) {
        curl_setopt($curly[$id], CURLOPT_POST,       1);
        curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
      }
    }
 
    // extra options?
    if (!empty($options)) {
      curl_setopt_array($curly[$id], $options);
    }
 
    curl_multi_add_handle($mh, $curly[$id]);
  }
 
  // execute the handles
  $running = null;
  do {
    curl_multi_exec($mh, $running);
  } while($running > 0);
 
 
  // get content and remove handles
  foreach($curly as $id => $c) {
    $result[$id] = curl_multi_getcontent($c);
    curl_multi_remove_handle($mh, $c);
  }
 
  // all done
  curl_multi_close($mh);
 
  return $result;

 $result = null;
  unset($result);
    gc_collect_cycles();
  gc_disable();
}
 
 for ($alx = 0; $alx <= 5 ; $alx++) {
	 gc_enable();
	 $nextTMName = 'tm-name/e'.$alx.'.txt';
 $homepage = file_get_contents($nextTMName);
$ins = json_decode ( $homepage , true);
 
 $r = multiRequest($ins); 
 
$json= json_encode($r);
$nextTMContent = 'tm-content/e'.$alx.'.txt';
$f = fopen($nextTMContent, "w");
fwrite($f, $json); 
fclose($f);
$r = null;
$json = null;
$nextTMName = null;
$homepage = null;
$ins = null;
$nextTMContent = null;
unset($r);
unset($json);
unset ($nextTMName);
unset ($homepage);
unset ($ins);
unset ($nextTMName);
gc_collect_cycles();
gc_disable();
 }
 $tm = microtime(true) - $start;
 echo $tm;
 echo "<hr>";
 echo 'Size ',$before-memory_get_usage(),' bait';
 echo "<hr>";

от сюда идут массивы с url для парсинга $nextTMName = 'tm-name/e'.$alx.'.txt';
в каждом .txt файле по 5к адресов .
Сюда сохраняется контент после парсинга $nextTMContent = 'tm-content/e'.$alx.'.txt';.
Удаляю переменые через unset , вызываю gc_collect_cycles() но всё равно после 5к запарсеных url выводит
500 Internal Server Error .
Почему gc_collect_cycles() не срабатывает должна очищать память ?
  • Вопрос задан
  • 620 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы