About PHP Memory release issues
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 a 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 Bo Yuan), it is not difficult to analyze the problem is how to release memory in the loop synchronously, instead of looping hundreds of to release the time, As you will find in debugging, if we wrap the code in the loop with high reusability in the function, and then use the form called sub-function, it will be the execution speed 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 startsWhen 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. "What we need to do most of all is to reduce the array of data that was originally stored, in the example above, releasing the processed elements in the array in a timely fashion, so that as the loop, the memory footprint will always fluctuate (the memory-recovery mechanism), but it will not grow, and it will achieve our initial goal. 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 immortal also no way, haha.
http://www.bkjia.com/PHPjc/972220.html www.bkjia.com true http://www.bkjia.com/PHPjc/972220.html techarticle about PHP Memory release issues 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, one-time ...