About PHP Memory release issues

Source: Internet
Author: User
Tags array 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 PHP memory overflow problem, the first two methods do not mention (you can refer to "About PHP memory release problem" in Bo Park), it is not difficult to analyze, in fact, the crux of the problem is how to synchronize the release of memory in the loop, Instead of looping hundreds of to release it once, because everyone in debugging will find that if we encapsulate the high reusability of the code into the function, and then use the form of calling the child function, the execution speed of the program will be reduced, about dozens of times times, this decline will vary with the amount of data.       loops nested in the form of processing large amounts of data in arrays, there are many uses of unset ($a), the form of a 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 aided byThe fourth member of the Zval (it has several forms) refcount to be resolved. 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 is able to see it from the RefCount parameter, and others are interested in the data, so it should just reduce the refcount count and then stop. "          Comprehensive, the most we need to do is to reduce the initial storage of the data array, the above example, in the loop in time to release the array of processed elements, so with the loop, The amount of memory consumed is always up and down (memory recovery mechanism problem), but it does not always grow, and it achieves 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 have no way, haha.

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.