我已经完成为我正在执行的项目编写我的php脚本。我的下一步是我想看看是否可以从内存的角度来改进我的代码,因为我的一些脚本吃了很多内存。我一直在研究这个,一个建议是NULL和未设置变量,但我从来没有看到这样做的例子。所以,我想给我的剧本做了共同行动的例子,想知道这是否是这样做的正确方法:在php脚本中释放内存
$query = $dbconn->get_results("SELECT id,name FROM account WHERE active = 1");
if(isset($query))
{
foreach($query AS $currq)
{
$account_id = intval($currq->id);
$account_name = trim($currq->name);
//Code to stuff with this data
//NULL the variables before looping again
$account_id = NULL;
$account_name = NULL;
//Unset the variables before looping again
unset($account_id);
unset($account_name);
}
$query = NULL;
unset($query);
$currq = NULL;
unset($currq);
请问这是为了释放内存的正确方法?我读了PHP中的垃圾收集可能很懒,所以他们推荐NULL值,因为它会立即缩小它的值。
我知道这个网站可能太模糊了,但是如果有人能让我知道这是释放内存的正确方法吗?或者,如果有不同的方式,您可以举一个例子,以便我可以直观地看到它是如何工作的。提前致谢!
什么是$ dbconn?自定义数据库类?看起来不像mysqli或PDO? – SirNarsh
这应该是张贴在http://codereview.stackexchange.com/ – nogad
你没有看到很多的例子,其几乎所有的情况下,它的无意义 – nogad