mysqli_result :: free增加php的内存使用量 [英] mysqli_result::free increase php memory usage

查看:65
本文介绍了mysqli_result :: free增加php的内存使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时memory_get_usage()在使用free()后返回较大的值.请参见下面的示例:

Sometimes memory_get_usage() return a bigger value after using free(). See the example below:

$query = "SELECT * FROM table";
$result = self::query($query);

while ($row = $result->fetch_assoc())
{
    $myArray[] = $row;
}

echo "Before: ".number_format(memory_get_usage());
$result->free();
echo "After: ".number_format(memory_get_usage());

输出为:

Before: 1,203,856
After: 1,370,976

但是,如果我在循环中注释该指令,则在free()之后内存使用量会减少:

But if I comment the instruction in the loop then the memory usage decrease after the free():

$result = self::query($query);

while ($row = $result->fetch_assoc())
{
    //$myArray[] = $row;
}

echo "Before: ".number_format(memory_get_usage());
$result->free();
echo "After: ".number_format(memory_get_usage());

赠予:

Before: 593,120
After: 325,448

结果很大(~Mb)并且有很多文本.只有几百行.

The result is quite big (~Mb) and there is a lot of text. It' only a few hundreds lines.

如果结果少于10行,则内存使用量始终会减少.

If the result has less than ~10 lines the memory usage always decrease.

我的猜测是free()释放了MySQL而不是php的内存,但是为什么它通常会减少php的内存使用量?

My guess was that free() free the memory of MySQL and not php but then why does it usually decrease the php memory usage?

有人可以解释这里发生了什么吗?

Can somebody explains what's going on here?

推荐答案

如果您使用的是mysql,而不是mysqli,请注意:

If you are using mysql, rather than mysqli, take note of here:

http://dev.mysql. com/doc/apis-php/zh-CN/apis-php-mysqlnd.stats.html

请注意,mysqlnd(与MySQL客户端库不同)尊重PHP 内存限制,因为它使用PHP内部内存管理功能 分配内存.这也是为什么memory_get_usage 报告使用mysqlnd而不是mysqlnd时更高的内存消耗 MySQL客户端库. memory_get_usage不测量内存 完全消耗MySQL客户端库,因为MySQL 客户端库不使用PHP内部内存管理功能 由功能监控!

Note that mysqlnd (unlike the MySQL Client Library) respects the PHP memory limit because it uses PHP internal memory management functions to allocate memory. This is also the reason why memory_get_usage reports a higher memory consumption when using mysqlnd instead of the MySQL Client Library. memory_get_usage does not measure the memory consumption of the MySQL Client Library at all because the MySQL Client Library does not use PHP internal memory management functions monitored by the function!

我的最佳猜测是:虽然缓冲结果集仍存在于MySQL库中,但您的$ myArray引用了相同的内存,因此不认为已使用内存.但是,一旦释放了结果集,内存就变成拥有的"或以其他方式转移到您的阵列,并且现在对内存的使用进行计数.

My best guess is: While the buffered result set still exists within the MySQL library, your $myArray has a reference to the same memory and thus memory is not considered used. However once you free the result set, the memory becomes "owned" or is otherwise transferred to your array and the memory usage is now counted.

这可能不是完全正确或完整的故事,但是我很有把握,这是问题的总要旨.

This may not be entirely correct or the full story, but I would be fairly confident that this is the general gist of the issue.

这篇关于mysqli_result :: free增加php的内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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