Skip to content

Commit

Permalink
删除内存清理, 因为用了Dom扩展,内存释放的问题已经解决
Browse files Browse the repository at this point in the history
  • Loading branch information
bupt1987 committed Apr 21, 2016
1 parent e16e6f0 commit 60f207e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 55 deletions.
22 changes: 0 additions & 22 deletions src/ParserDom.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,6 @@ function __get($name) {
}
}

/**
* @codeCoverageIgnore
*/
public function __destruct() {
$this->clearNode($this->node);
}

/**
* 深度优先查询
*
Expand Down Expand Up @@ -378,19 +371,4 @@ private function getParent($node) {
return $node->parentNode;
}

/**
* @codeCoverageIgnore
* 释放内存
*
* @param $node
*/
private function clearNode(&$node) {
if (!empty($node->childNodes)) {
foreach ($node->childNodes as $child) {
$this->clearNode($child);
}
}
unset($node);
}

}
50 changes: 17 additions & 33 deletions test.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
<?php
require 'vendor/autoload.php';

$html = '<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
<p class="test_class test_class1">p1</p>
<p class="test_class test_class2">p2</p>
<p class="test_class test_class3">p3</p>
<div id="test1"><span style="display: none">测试1<br/></span><input date=\'"sdfsf"\' name="test" value="123"/>123123</div>
</body>
</html>';

$html_dom = new \HtmlParser\ParserDom($html);
$p_array = $html_dom->find('p.test_class');
$p1 = $html_dom->find('p.test_class1',0);
$div = $html_dom->find('div#test1',0);
foreach ($p_array as $p){
echo $p->getPlainText() . "\n";
}
echo $div->getPlainText() . "\n";
echo $p1->getPlainText() . "\n";
echo $p1->getAttr('class') . "\n";

echo "show html:\n";
echo $div->innerHtml() . "\n";
echo $div->outerHtml() . "\n\n";


$url = 'http://www.sina.com.cn/';
$sHtml = file_get_contents($url);

$oDom = new \HtmlParser\ParserDom($sHtml);
$oFound = $oDom->find('ul.uni-blk-list02', 0);
for($i = 0; $i < 10000; $i ++) {
test($sHtml);
if($i % 100 == 0) {
echo $i . ' ';
echo round(memory_get_usage() / 1024 / 1024, 3) . 'M, ';
echo round(memory_get_peak_usage() / 1024 / 1024, 3) . 'M' . "\n";
}
}

echo "inner:\n\n" . $oFound->innerHtml() . "\n\n";
echo "outer:\n\n" .$oFound->outerHtml() . "\n";
echo round(memory_get_usage() / 1024 / 1024, 3) . 'M, ';
echo round(memory_get_peak_usage() / 1024 / 1024, 3) . 'M' . "\n";

function test($sHtml) {
$oDom = new \HtmlParser\ParserDom($sHtml);
$oDom->find('ul.uni-blk-list02', 0);
$oDom->find('a');
$oDom->find('ul');
$oDom->find('p');
}

0 comments on commit 60f207e

Please sign in to comment.