简体   繁体   English

PHP脚本内存泄漏直到最终溢出

[英]PHP Script Memory Leaking Until Eventual Overflow

Keep running into a weird problem with a PHP script. 使用PHP脚本继续遇到一个奇怪的问题。 The idea of the script is to grab a bunch of data from a MySQL table, process it, compare it some data from another table, and print out the results to a file in CSV format. 该脚本的想法是从MySQL表中获取一堆数据,处理它,比较来自另一个表的一些数据,并将结果打印成CSV格式的文件。

This script will run several hundreds of thousands of iterations to complete. 该脚本将运行数十万次迭代才能完成。 Problem I have is that even with using unset() on the arrays and free() on my MySQL queries, memory continues to be used up until around iteration 350,000 or so it runs out of memory. 我遇到的问题是即使在我的MySQL查询中使用数组上的unset()和我的MySQL查询中的free(),内存仍然会被使用,直到大约350,000左右,内存耗尽。

Without fail, every time I run the script the memory allocation as reported by memory_get_usage() stays the same until approximately ever 1050 iterations. 没有失败,每次我运行脚本时,memory_get_usage()报告的内存分配保持不变,直到大约1050次迭代。 Then it will increase by exactly 262,144 bytes. 然后它将增加262,144字节。 Through testing it also always increments in the exact same place in the script. 通过测试,它也总是在脚本中完全相同的位置递增。 In the code block below it will always increment in one of the iterations of b2a (maximum of 4 possible iterations). 在下面的代码块中,它总是在b2a的一次迭代中递增(最多4次可能的迭代)。

//up here we get the data to run analysis/comparisons on.

echo "{$lineCounter}b:  " . memory_get_usage(TRUE) . "\n"; //27525120
if ($AttResults = $db_cx->query($ObsAttQuery)) {
   echo "{$lineCounter}b1:  " . memory_get_usage(TRUE) . "\n"; //27525120
   $attArray = array();
   echo "{$lineCounter}b2: " . memory_get_usage(TRUE) . "\n"; //27525120
   while ($attRow = $AttResults->fetch_object()) {
      echo "{$lineCounter}b2a {$attRow->attId}: " . memory_get_usage(TRUE) . "\n"; //27787264
      $attArray[$attRow->attId] = $attRow->value;
   }
echo "{$lineCounter}b3: " . memory_get_usage(TRUE) . "\n"; //27787264
$AttResults->free();
if (empty($attArray)) continue;

//down here after it is done I have an unset on the $attArray variable

Any suggestions on things to try?? 关于要尝试的事情的任何建议??

Prior to version 5.3 php had a very stupid garbage collector: Memory Leaks With Objects in PHP 5 . 在版本5.3之前,php有一个非常愚蠢的垃圾收集器: PHP 5中的内存泄漏对象 With this you could not even free your memory with unset() . 有了这个,你甚至无法用unset()释放你的记忆。 You have to break circular references before you unset, or simply use at least php 5.3. 你必须在取消设置之前打破循环引用,或者至少使用php 5.3。

Did you look at the data retrieved by: 您是否查看了检索到的数据:

$attRow = $AttResults->fetch_object()

before/around iteration 1050 ? 迭代1050之前/周围?

...and... yes, I would change the assignment in the while with one fetch before the loop and one before the end to see if the memory changes. ......并且......是的,我会在while中更改赋值,在循环之前进行一次提取,在结束之前进行一次提取以查看内存是否发生更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM