Я запускаю код PHP ниже из командной строки.Проблема в том, что его потребление памяти намного больше, чем должно быть.Насколько я могу судить, я не могу понять, где потребляется память.
for ($i=0;$i<100;$i++)
{
$classObject = $classObjects[$i];
echo $i . " : " . memory_get_usage(true) . "\n";
$classDOM = $scraper->scrapeClassInfo($classObject,$termMap,$subjectMap);
unset($classDOM);
}
По моему мнению, память, используемая моим сценарием, должна оставаться более или менее постоянной после каждой итерациипетля.Любая память, занятая $scraper->scrapeClassInfo()
, должна быть освобождена, когда ее члены выходят из области видимости.
Это выходной файл, который я получаю.Для краткости я показываю каждую десятую строку вывода:
0 : 5767168
10 : 12058624
20 : 18350080
30 : 24903680
40 : 30932992
50 : 37748736
60 : 43778048
70 : 49807360
80 : 55836672
90 : 62914560
97 : 66846720
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 44 bytes) in /home/content/60/8349160/html/drexel/simple_html_dom.php on line 1255
Наконец, насколько я понимаю, то, что делает $scraper->scrapeClassInfo()
, на самом деле не должно быть виновником, а простона случай, вот код:
function scrapeClassInfo($class,$termMap,$subjectMap)
{
$ckfile = tempnam ("/tmp", "CURLCOOKIE");
$ckfile2 = tempnam ("/tmp", "CURLCOOKIE2");
$ckfile3 = tempnam ("/tmp", "CURLCOOKIE3");
$termpage = $termMap[$class['termcode']];
$subjectpage = $subjectMap[$class['subjectcode']];
$classpage = $class['classlink'];
//hit the main page and get cookie
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $this->mainURL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);
//hit the term page and get cookie
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $termpage);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);
//hit the subject page and get cookie
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile3);
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $subjectpage);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
curl_close($ch);
//hit the class page and scrape
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $classpage);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
return str_get_html($result);
}
Метод, вызванный в последней строке, str_get_html()
является членом Simple HTML DOM Parser
Если этоважно, вот как я называю свой сценарий:
/usr/local/php5/bin/php index.php 2>&1 1>output