PHP garbage collection sucks or is it just me?

I have a function below which I call very often in a loop.

I waited 5 minutes when the memory rose from 1MB to 156MB. Should the PHP garage builder appear and shrink at some point ?!

Is it because I set the memory limit to 256 MB?

At 2,3,4 echo point, this is a fairly constant use of memory. It falls on my half mega at point 4. But point 1 is where the increase in main memory occurs. Probably due to file_get_html loading the html file into memory.

If I understood and did not understand the $html variable, will this take care of this?

 function get_stuff($link, $category ){ $html = file_get_html(trim("$link")); $article = $html->find('div[class=searchresultsWidget]', 0); echo '1 - > '.convert(memory_get_usage(true)).'<br />'; foreach($article->find('h4 a') as $link){ $next_url = 'http://new.mysite.com'.$link->href; $font_name = trim($link->plaintext); $html = file_get_html(trim("$next_url")); $article = $html->find('form[class=addtags]', 0); $font_tags = ''; foreach($article->find('ul[class=everyone_tags] li a span') as $link){ $font_tags .= trim($link->innertext).','; } echo '2 - > '.convert(memory_get_usage(true)).'<br />'; $font_name = mysql_real_escape_string($font_name); $category = mysql_real_escape_string($category); $font_tags = mysql_real_escape_string($font_tags); $sql = "INSERT INTO tag_data (font_name, category, tags) VALUES ('$font_name', '$category', '$font_tags')"; unset($font_tags); unset($font_name); unset($category); $html->clear(); mysql_query($sql); unset($sql); echo '3 - > '.convert(memory_get_usage(true)).'<br />'; } unset($next_url); unset($link); $html->clear(); unset($html); unset($article); echo '4 - > '.convert(memory_get_usage(true)).'<br />'; } 

As you can see, I tried to use unset weakly. Although this is not good, because I understand that he will not "cancel" the memory as soon as I call it.

Thanks to everyone for any help on how I can reduce this upward memory growth.

+7
garbage-collection php memory
source share
3 answers

A memory leak with the get_html () file is known there: http://simplehtmldom.sourceforge.net/manual_faq.htm#memory_leak

The solution is to use

 $html->clear(); 

What you do, BUT: you use $ html both inside and outside the loop. Inside the loop, you call $ html-> clear (), and then near the end of your function $ html-> clear () again (I assume I will catch your original link to the file_get_html () object). This last call does nothing. You skip memory with the initial call to $html = file_get_html() .

Try using another variable ($ html1, maybe?) Inside your loop and see what happens.

+8
source share

The goal of the garbage collector is solely to catch round links.

If they are not present, the variables are immediately eliminated after their number of references reaches 0.

I do not recommend using unset , except in exceptional cases. Use functions instead and rely on variables to go out of scope to recover memory.

In addition, we cannot describe to you what exactly is happening, because we will need to know exactly what a simple DOM parser does. There may be round links or global resources containing the link, but this will be difficult to understand.

See the basics of link counting and collection cycles .

+3
source share

PHP did not have a proper garbage collector until 5.3 . Basically, he used only reference counting, which would leave circular links until the script exits (for example, $a =& $a is circular). In addition, the clear code, which is its DID, will only work if memory pressure is required. for example, you do not need to do an expensive cleaning cycle if a new freed memory is not needed.

As in 5.3, there is a proper garbage collector, and you can get it to work with gc_enable() and gc_collect_cycles() .

+2
source share

All Articles