PHP脚本中的内存泄漏 [英] memory leak in php script
问题描述
我有一个运行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.