Why does php report a memory overflow after running for a while?

Source: Internet
Author: User
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 +

  • The yield keyword is used in the loop. the intermediate variable in the iteration can not occupy additional memory space.

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.

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.