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

WBOY
Release: 2023-03-02 07:02:01
Original
1516 people have browsed it

I read 100,000 pieces of data from the database at one time, and then did some calculations in a loop. During the loop, the variables involved will be overwritten by the next loop. The current situation is that after the program has been running for a period of time, a memory overflow is reported. , why didn't it report memory overflow at the beginning? According to my understanding, I did not use variables to store the operation results, so if there was a memory overflow, it should have been reported at the beginning.

Reply content:

I read 100,000 pieces of data from the database at one time, and then did some calculations in a loop. During the loop, the variables involved will be overwritten by the next loop. The current situation is that after the program has been running for a period of time, a memory overflow is reported. , why didn't it report memory overflow at the beginning? According to my understanding, I did not use variables to store the operation results, so if there was a memory overflow, it should have been reported at the beginning.

php version 5.5+

  • Use the yield keyword in the loop, and the intermediate variables in the iteration do not occupy additional memory space

Example:

<code>for($i = 1; $i <= 10; $i += 1)
        yield $i;</code>
Copy after login

In fact, in php, variables are saved through zval variables. zend_uint refcount__gc is a counter in this variable to save how many variables there are. When a variable is generated, its refcount=1. Typical assignment operations such as $a = $b will increase the refcount of zval by 1, and the unset operation will decrease it by 1 accordingly. Before PHP5.3, the reference counting mechanism was used to implement GC. If the refcount of a zval was less than 0, then the Zend engine would think that there was no variable pointing to the zval, so it would release the memory space occupied by the zval. But, sometimes things are not that simple. We will see later that the simple reference counting mechanism cannot GC the circularly referenced zval, even if the variable pointing to the zval has been unset, resulting in a memory leak.

When you overwrite variables in a loop, the actual recount value does not decrease, so the memory occupied is not released, and it will definitely explode in the end.

Solution:

<code>ini_set(‘memory_limit’,’1024M’);
</code>
Copy after login

Problem analysis:

<code>=。=没代码怎么分析。。。
</code>
Copy after login

Possible reasons:

<code>读取数据不要时间啊?
变量覆盖之前生成的临时对象就不占内存啊?</code>
Copy after login

When you read 100,000 pieces of data from the database, you may have run out of memory

I have the impression that when variables are reassigned, the object data in the memory will actually be COPYed and will be released only by unset or GC, but I have not found relevant information.
There are many factors that occupy memory. PHP is a dynamic language, and memory management operations are hidden. If you use the same variable, it seems to use the same memory area, but in fact it is not.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!