Php memory release Problems
I took a morning to look at the code that previously solved the memory problem. Relatively speaking, I am very dissatisfied with the optimization program of my code, importing 40 thousand pieces of data at a time makes the code so cumbersome. I don't think this is a fundamental solution. Through online retrieval, further analysis of the problem: in the case of php memory overflow, the first two methods are not mentioned (you can refer to the php memory release issue in the Expo). It is not difficult to find out that the key to the problem is how to release the memory synchronously in a loop, instead of repeating hundreds of records to release the code once, you will find that if we encapsulate code with high reusability in the loop into the function, and then use the form of calling the subfunction, the program execution speed will decrease, about dozens of times, which will change with the amount of data. In the form of loop nesting, many of the arrays that process large amounts of data use unset ($ a) to release the memory in time, but in fact this is meaningless, reference the following analysis: "in the engine, variable names and their values are actually two different concepts. The value itself is an unknown zval * storage body (in this example, it is a string value) and is assigned to the variable $ a through zend_hash_add. What happens if both 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 (""), & helloval, sizeof (zval *), NULL); zend_hash_add (EG (active_symbol_table), "B", sizeof ("B"), & helloval, sizeof (zval *), NULL);} at this time, you can actually observe $ a or $ B, and you will see that they all contain the string "Hello World ". Unfortunately, next, you will continue to execute the third line of code "unset ($ );". At this time, unset () does not know that the data pointed to by the $ a variable is also used by another variable, so it just blindly releases the memory. Any subsequent access to variable $ B will be analyzed as the released memory space and thus cause the engine to crash. This problem can be solved by using refcount, the fourth member of zval (which has several forms. When a variable is created and assigned a value for the first time, its refcount is initialized to 1 because it is assumed to be used only by the corresponding variable when it was initially created. When your code snippet begins to assign helloval to $ B, it needs to increase the refcount value to 2; therefore, this 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 (""), & 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 $ a replica of the original variable, it will be able to see from the refcount parameter, there are other persons Therefore, it should only reduce the refcount Count value and no longer care about it. "In summary, what we need to do most is actually to reduce the array of originally stored data. In the above example, elements that have been processed in the array are released in a timely manner in the loop, so that with the loop, the memory usage will keep fluctuating (memory recovery mechanism problems), but it will not keep increasing, which has achieved our initial goal. Of course, the maximum data processing capacity at a time depends on the memory allocated by the server to php. Reading an array of data at a time exceeds the limit, and there is no way for the gods to read it. Haha.