Zil*_*ilk 6 php postgresql memory-leaks
PHP文档有这样的说法pg_free_result():
只有在脚本执行期间的内存消耗成为问题时才需要调用此函数.否则,脚本结束时将自动释放所有结果内存.
http://www.php.net/manual/en/function.pg-free-result.php
我(也许是天真的)已经预料到调用返回的资源pg_query()在超出范围时会被垃圾收集.
在这样的假设函数中:
function selectSomething ()
{
$res = pg_query("SELECT blah FROM sometable");
// do something with $res
pg_free_result($res); // required or not?
}
Run Code Online (Sandbox Code Playgroud)
是否真的有必要pg_free_result()在最后打电话?
换句话说,如果我将此函数调用1000次,它会占用内存来存储所有1000个结果吗?
编辑:我在谈论典型案例,即pg_connect()代替pg_pconnect().
这是我的测试结果:
(之前/之后)
与:631288 / 631384
无:631288 / 631640
请随意使用以下代码自行运行测试:
<?php
class test {
private static $tests = 5000;
public function __construct() {
$dbconn = pg_connect("host=### dbname=### user=### password=###")
or die('Could not connect: ' . pg_last_error());
self::test(self::$tests, true);
self::test(self::$tests, false);
}
private function test($times, $with) {
echo ($with ? "with:<br />\n" : "without:<br />\n") . memory_get_usage() ."<br />\n";
for($i = 0; $i < $times; $i++) {
$res = pg_query("SELECT * FROM chowder");
if($with) {
pg_free_result($res);
}
}
echo memory_get_usage() ."<br /><br />\n\n";
}
}
$test = new test();
Run Code Online (Sandbox Code Playgroud)