I read 0.1 million pieces of data from the database at a time, and then perform some computation cyclically. during The loop process, all the variables involved will be overwritten by the next loop. now, after the program runs for a while, I reported a memory overflow. Why didn't I report a memory overflow at the beginning? According to my understanding... I read 0.1 million pieces of data from the database at a time, and then perform some computation cyclically. during The loop process, all the variables involved will be overwritten by the next loop. now, after the program runs for a while, I reported a memory overflow. Why didn't I report a memory overflow at the beginning? According to my understanding, I didn't use variables to store the operation results all the time. so if the memory overflows, I should have reported it in the beginning.
Reply content:
I read 0.1 million pieces of data from the database at a time, and then perform some computation cyclically. during The loop process, all the variables involved will be overwritten by the next loop. now, after the program runs for a while, I reported a memory overflow. Why didn't I report a memory overflow at the beginning? According to my understanding, I didn't use variables to store the operation results all the time. so if the memory overflows, I should have reported it in the beginning.
Php version 5.5 +
Example:
for($i = 1; $i <= 10; $i += 1) yield $i;
In php, all the real variables are saved through the zval variable. zend_uint refcount _ gc is a counter in the variable to save the number of variables. When a variable is generated, its refcount = 1. a typical value assignment operation, for example, $ a = $ B, will increase the refcount of zval by 1, while the unset operation will subtract 1. Before PHP5.3, the reference counting mechanism was used to implement GC. if the refcount of a zval is less than 0, the Zend Engine considers that no variable points to the zval, therefore, the memory occupied by the zval will be released. However, things are sometimes not that simple. Later we will see that the reference counting mechanism alone cannot GC the zval of circular references, even if the variable pointing to this zval has been unset, leading to memory leakage.
When you cyclically override the variable, the essential recount value is not actually reduced, so the occupied memory is not released, and it will definitely explode in the end.
Solution:
ini_set(‘memory_limit’,’1024M’);
Problem analysis:
=. = No code for analysis...
Possible causes:
Do not read data for time? When a variable overwrites the previously generated temporary object, it does not occupy the memory?
When you read 10 million data records from the database, it is estimated that the memory is insufficient.
In my opinion, the variable is re-assigned, and the object data in the memory is actually copied, and unset or GC is used to release the data. However, I did not find the relevant information.
There are many memory usage factors. PHP is a dynamic language, and memory management operations are hidden. Using the same variable seems to use the same memory region, but this is not the case.