About the PHP memory release problem (ii)

Source: Internet
Author: User
Tags array continue string variable

Today I smoked a morning to look at the previous solution to the memory problem of the code, relatively speaking, I am very dissatisfied with the code optimization program, one import 40,000 of data to make the code so cumbersome, I think this is not the fundamental solution. Through the on-line search, the question has the further analysis:

In the issue of PHP memory overflow, the first two methods do not mention (you can refer to the "About PHP memory release problem" in Bo Yuan), it is not difficult to analyze, in fact, the crux of the problem is how to release the memory in the loop, rather than the circulation of hundreds of to release once, because you will find in the debugging, If we encapsulate the high reusability code in the loop into a function, and then use the form of calling a child function, the execution rate of the program will be reduced by about dozens of times times, and the decrease will vary with the amount of data.

Loops are nested in the form of processing large amounts of data in an array, there are many uses of unset ($a), the form of timely release of memory, but in fact this does not make sense, citing the following analysis:

"In the engine, variable names and their values are actually two different concepts." The value itself is an unnamed zval* storage body (in this case, a string value) that is assigned to the variable $a by Zend_hash_add (). What happens if two variable names point to the same value?

{
Zval *helloval;
Make_std_zval (Helloval);
Zval_string (Helloval, "Hello World", 1);
Zend_hash_add (EG (active_symbol_table), "a", sizeof ("a"), &helloval, sizeof (zval*), NULL);
Zend_hash_add (EG (active_symbol_table), "B", sizeof ("B"), &helloval, sizeof (zval*), NULL);
}


At this point, you can actually observe $a or $b, and you'll see that they all contain the string "Hello world". Unfortunately, next, you continue to execute the third line of code "unset ($a);" At this point, unset () does not know that the data that the $a variable points to is also used by another variable, so it simply releases the memory blindly. Any subsequent access to the variable $b will be parsed into the freed memory space and thus cause the engine to crash.

This problem can be solved by means of the fourth member refcount of Zval (which has several forms). When a variable is first created and assigned a value, its refcount is initialized to 1 because it is assumed to be used only by the corresponding variable when it was originally created. When your code snippet starts assigning Helloval to $b, it needs to increase the value of RefCount to 2, so that the value is now referenced by two variables:

{
Zval *helloval;
Make_std_zval (Helloval);
Zval_string (Helloval, "Hello World", 1);
Zend_hash_add (EG (active_symbol_table), "a", sizeof ("a"), &helloval, sizeof (zval*), NULL);
Zval_addref (Helloval);
Zend_hash_add (EG (active_symbol_table), "B", sizeof ("B"), &helloval,sizeof (zval*), NULL);
}


Now, when unset () deletes the corresponding copy of the $a of the original variable, it can be seen from the RefCount parameter, and others are interested in the data, so it should just reduce the refcount count, and then no longer manage it. “

In summary, the most we need to do is to reduce the initial array of stored data, the above example, in the loop in time to release the elements that have been processed in the array, so along with the cycle, memory consumption will continue to fluctuate up and down (memory recovery mechanism problem), but will not always grow, but also to achieve our original purpose. Of course, a one-time maximum amount of data processing or depends on the server to the memory allocated to PHP, a single data reading into the array is beyond the limit, which is immortal also no way, haha



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.