PHP脚本中的内存泄漏 [英] memory leak in php script

查看:91
本文介绍了PHP脚本中的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行mysql查询的php脚本,然后循环执行结果,并且在该循环中还运行了几个查询:

I have a php script that runs a mysql query, then loops the result, and in that loop also runs several queries:

    $sqlstr = "SELECT * FROM user_pred WHERE uprType != 2 AND uprTurn=$turn ORDER BY uprUserTeamIdFK";
    $utmres = mysql_query($sqlstr) or trigger_error($termerror = __FILE__." - ".__LINE__.": ".mysql_error());
    while($utmrow = mysql_fetch_array($utmres, MYSQL_ASSOC)) {
// some stuff happens here    
//  echo memory_get_usage() . " - 1241<br/>\n";
        $sqlstr = "UPDATE user_roundscores SET ursUpdDate=NOW(),ursScore=$score WHERE ursUserTeamIdFK=$userteamid";
        if(!mysql_query($sqlstr)) {
            $err_crit++;
            $cLog->WriteLogFile("Failed to UPDATE user_roundscores record for user $userid - teamuserid: $userteamid\n");
            echo "Failed to UPDATE user_roundscores record for user $userid - teamuserid: $userteamid<br>\n";
            break;
        }
    unset($sqlstr);
    //  echo memory_get_usage() . " - 1253<br/>\n";
// some stuff happens here too
}

更新查询永远不会失败.

The update query never fails.

由于某些原因,在两次调用memory_get_usage之间,添加了一些内存.因为大循环运行约500.000次或更多次,最终它实际上会增加大量内存.我在这里想念什么吗?
难道是不是实际上不是在两个调用之间添加了内存,而是在脚本的另一点上添加了内存?

For some reason, between the two calls of memory_get_usage, there is some memory added. Because the big loop runs about 500.000 or more times, in the end it really adds up to alot of memory. Is there anything I'm missing here?
could it herhaps be that the memory is not actually added between the two calls, but at another point in the script?

一些额外的信息: 循环之前大约为5mb,循环之后大约为440mb,每个更新查询大约增加250个字节. (其余的内存将添加到循环中的其他位置). 我之所以没有发布更多的其他内容",是因为它包含大约300行代码.我发布了此部分,因为它看起来是添加最多内存的地方.

some extra info: Before the loop it's at about 5mb, after the loop about 440mb, and every update query adds about 250 bytes. (the rest of the memory gets added at other places in the loop). The reason I didn't post more of the "other stuff" is because its about 300 lines of code. I posted this part because it looks to be where the most memory is added.

推荐答案

来自

参数

real_usage将此设置为TRUE可获取 从中分配的实际内存大小 系统.如果未设置或仅设置为FALSE 报告emalloc()使用的内存.

real_usage Set this to TRUE to get the real size of memory allocated from system. If not set or FALSE only the memory used by emalloc() is reported.

将此参数设置为true时,脚本不会像我期望的那样显示内存增加.

With this parameter set to true, the script showed no increase of memory, like I expected.

这篇关于PHP脚本中的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆