About the php memory release issue. I took a morning to look at the code that solved the memory problem in php. I am not satisfied with the optimization program of my code, one-time questions about php memory release
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. Further analysis of the problem through online search:
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 blog, in fact, the key to the problem is how to release the memory synchronously in a loop, instead of releasing several hundred records in a loop, because we will find that during debugging, if we encapsulate code with high reusability in a loop into a function, and then use the form of calling subfunctions, the execution speed of the program will be reduced, about dozens of times, this decline 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 ("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 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 ("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 $ A replica of the original variable, it will be able to see from the refcount parameter that other people are interested in the data; therefore, it should only reduce the refcount count value, and then ignore it. "
To sum up, what we need to do most is to reduce the array that originally stores data. in the above example, the 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.
Parse 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, one-time...