简体   繁体   English

在php脚本中释放内存

[英]Freeing up memory in php script

I have finished writing my php scripts for a project I am doing. 我已经为正在执行的项目编写了php脚本。 My next step is I would like to see if I can improve my code from a memory stand point as some of my scripts eat a lot of memory. 下一步是我想从内存的角度看我是否可以改善代码,因为我的一些脚本占用了大量内存。 I have been doing research on this and one suggestion is to NULL and unset variables, but I never see an example of doing this. 我一直在对此进行研究,一个建议是对NULL和未设置变量,但我从未见过这样做的示例。 So I wanted to give an example of a common action done in my scripts and wanted to know if this is the proper way of doing this: 因此,我想举一个在脚本中完成的常见操作的示例,并想知道这是否是执行此操作的正确方法:

    $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);

Would that be the correct way to free up memory? 那是释放内存的正确方法吗? I read the garbage collection in PHP can be lazy, so that is why they recommend to NULL the value as it will shrink it right away. 我读到PHP中的垃圾回收可能很懒,因此这就是为什么他们建议将NULL值设为NULL的原因,因为它将立即缩小它。

I know this might be too vague for this site, but if anyone can just let me know if this is the proper way of freeing up memory? 我知道这对于该站点可能太含糊,但是是否有人可以让我知道这是否是释放内存的正确方法? Or if there is a different way, can you please provide an example just so I can visually see how it work. 或者,如果有其他方法,请提供一个示例,以便我可以直观地看到其工作方式。 Thanks in advance! 提前致谢!

Please read up on PHP generators , that is what they are exactly for. 请仔细阅读PHP生成器 ,这就是它们的确切用途。

You don't want to fetch all records at once, this would blow holes into your memory like a shotgun. 您不想一次获取所有记录,这会像a弹枪一样在您的内存中造成漏洞。

Instead you want to fetch your records one at the time, process it then fetch the next one. 相反,您想一次获取一个记录,然后对其进行处理,然后获取下一个记录。

Here is an example: 这是一个例子:

function getAccountData(\PDO $pdo)
{
    $stmt = $pdo->prepare("SELECT id,name FROM account WHERE active = 1");
    $stmt->execute();

    while ($row = $stmt->fetch()) {
        yield $row;
    }
}



foreach (getAccountData($pdo) as $account){
    //process the record for each iteration
    //no need to unset anything
}

Well, if the function $dbconn->get_results returns an array with all the data, then there is no point in using generators since the memory has already being allocated for the data. 好吧,如果函数$ dbconn-> get_results返回一个包含所有数据的数组,那么使用生成器毫无意义,因为已经为数据分配了内存。

You can also use the mysqli_fetch_assoc function to get one row at a time. 您也可以使用mysqli_fetch_assoc函数一次获取一行。 It should be more memory efficient then fetching all rows at once. 与一次提取所有行相比,它应该具有更高的内存效率。 http://php.net/manual/en/mysqli-result.fetch-assoc.php http://php.net/manual/en/mysqli-result.fetch-assoc.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM