Skip to content

Commit 60f207e

Browse files
committed
删除内存清理, 因为用了Dom扩展,内存释放的问题已经解决
1 parent e16e6f0 commit 60f207e

File tree

2 files changed

+17
-55
lines changed

2 files changed

+17
-55
lines changed

src/ParserDom.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,6 @@ function __get($name) {
8484
}
8585
}
8686

87-
/**
88-
* @codeCoverageIgnore
89-
*/
90-
public function __destruct() {
91-
$this->clearNode($this->node);
92-
}
93-
9487
/**
9588
* 深度优先查询
9689
*
@@ -378,19 +371,4 @@ private function getParent($node) {
378371
return $node->parentNode;
379372
}
380373

381-
/**
382-
* @codeCoverageIgnore
383-
* 释放内存
384-
*
385-
* @param $node
386-
*/
387-
private function clearNode(&$node) {
388-
if (!empty($node->childNodes)) {
389-
foreach ($node->childNodes as $child) {
390-
$this->clearNode($child);
391-
}
392-
}
393-
unset($node);
394-
}
395-
396374
}

test.php

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,25 @@
11
<?php
22
require 'vendor/autoload.php';
33

4-
$html = '<html>
5-
<head>
6-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
7-
<title>test</title>
8-
</head>
9-
<body>
10-
<p class="test_class test_class1">p1</p>
11-
<p class="test_class test_class2">p2</p>
12-
<p class="test_class test_class3">p3</p>
13-
<div id="test1"><span style="display: none">测试1<br/></span><input date=\'"sdfsf"\' name="test" value="123"/>123123</div>
14-
</body>
15-
</html>';
16-
17-
$html_dom = new \HtmlParser\ParserDom($html);
18-
$p_array = $html_dom->find('p.test_class');
19-
$p1 = $html_dom->find('p.test_class1',0);
20-
$div = $html_dom->find('div#test1',0);
21-
foreach ($p_array as $p){
22-
echo $p->getPlainText() . "\n";
23-
}
24-
echo $div->getPlainText() . "\n";
25-
echo $p1->getPlainText() . "\n";
26-
echo $p1->getAttr('class') . "\n";
27-
28-
echo "show html:\n";
29-
echo $div->innerHtml() . "\n";
30-
echo $div->outerHtml() . "\n\n";
31-
32-
334
$url = 'http://www.sina.com.cn/';
345
$sHtml = file_get_contents($url);
356

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

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

19+
function test($sHtml) {
20+
$oDom = new \HtmlParser\ParserDom($sHtml);
21+
$oDom->find('ul.uni-blk-list02', 0);
22+
$oDom->find('a');
23+
$oDom->find('ul');
24+
$oDom->find('p');
25+
}

0 commit comments

Comments
 (0)