About PHP Memory release issues (ii) _php tutorial

Source: Internet
Author: User

About PHP Memory release issues (II)


Today smoked a morning time, to see the previous solution to the memory problem of the code, relatively speaking, I am very dissatisfied with the optimization of their code, a one-time import of 40,000 data to make the code become so cumbersome, I think this is not the fundamental solution. Through the online search, the problem has further analysis:

In the PHP memory overflow problem, the first two methods do not mention (can refer to the "About PHP memory release problem" in the Bo Garden), it is not difficult to analyze the problem is how to release memory synchronously in the loop, instead of looping hundreds of to release once, because we will find in the debugging, If we encapsulate the more reusable code in the loop into the function, and then use the form called sub-function, it will be the execution of the program is reduced, about dozens of times times, the decrease will vary with the amount of data.

In the form of looping nesting, there are many ways to free up memory in a way that handles large amounts of data in the form of unset ($a), but in practice it makes no sense to refer to the following analysis:

"In the engine, the variable names and their values are actually two different concepts. The value itself is an unnamed zval* storage (in this case, a string value), which is assigned to the variable $ A by Zend_hash_add (). What happens if two of the 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 pointed to by the $ A variable 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 with the help of a fourth member of the Zval (which has several forms) refcount. 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 $ A for the original variable, it can be seen from the RefCount parameter, and others are interested in that data, so it should just reduce the count of RefCount, and then no longer control it. “

In summary, the most we need to do is to reduce the initial storage of the data array, the above example, in the loop in a timely manner to release the array has been processed elements, so with the loop, memory consumption will always fluctuate (memory recovery mechanism problem), but not always grow, but also to achieve our original purpose. Of course, one-time maximum amount of data processing is still dependent on the server to PHP allocated memory, a single data volume read into the array is beyond the limit, which is the immortal has no way, haha

http://www.bkjia.com/PHPjc/973373.html www.bkjia.com true http://www.bkjia.com/PHPjc/973373.html techarticle about PHP Memory release issues (ii) today, I took a morning to look at the code that solved the memory problem before, relatively speaking, I am very dissatisfied with the optimizer of my own code ...

  • 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.