PHP为什么未设置不会使内存释放? [英] PHP why unset do not makes memory free?

查看:251
本文介绍了PHP为什么未设置不会使内存释放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php应用程序,该应用程序应该管理(导出)大量(大量)数据,并且必须在生产环境中进行...因此我需要使内存使用率尽可能低(主要标准).

I have php application which should manage (export) big (huge) amount of data, and it has to be done on production... so I need to make as low memory usage as possible (main criteria).

简而言之,App周期性地导出数据,例如

Shortly say App exporting data in cycle, like

for($fileCounter=0;$fileCounter<=70;$fileCounter++) {
... HERE a lot of (more than 1K lines) huge work, many variables a lot of DB queries from another databases etc ...
}

我不想在这里展示完整的逻辑,因为这可能会花很多时间给其他人,这不是这里的重点.

I don't want to show here full logic because it can take a lot of time for other peoples, it's not the main point here.

要点是,为什么如果我将在每次迭代期间unset()所有新创建的变量都不会减少内存使用量呢?像这样

Main point is, why if I will unset() all newly created variables during each iteration it does not decrease memory usage ? like this

for($fileCounter=0;$fileCounter<=70;$fileCounter++) {
    // optimization purpose
    $vars_at_start = array_keys(get_defined_vars());
    echo memory_get_peak_usage(true) . PHP_EOL;

... huge logic ...

    $vars_at_end = array_diff($vars_at_start, array_keys(get_defined_vars()));
    foreach($vars_at_end as $v) unset($v);
    unset($vars_at_end);
}

以及如何减少内存使用量?如果我需要使用很多查询,变量等.

and how I could decrease memory usage ? if I need to use so many queries, variebles etc..

P.S.代码不是我的代码:),并且我不想从头开始重写它,我只是在寻找优化方向.

P.S. code is not mine :) and I don't want to rewrite it from scratch, I'm just looking for optimization direction.

下一个不带变量的清理内存使用情况(它是在每次迭代的开始进行测量)

without variables cleaning memory usage is next (it is measuring in the beginning of each iteration)

23592960

Started: 0 - 12:58:26
Ended: 13:00:51
877920256 (difference 854'327'296)

Started: 1 - 13:00:51
Ended: 13:03:39
1559494656 (difference 681'574'400)

并带有变量清洗

23592960

Started: 0 - 12:47:57
Ended: 12:50:20
877920256 (difference 854'327'296)

Started: 1 - 12:50:20
Ended: 12:53:16
1559756800 (difference 681'836'544)

根据我的阅读,PHP有很多原因来泄漏内存...像这样的 https ://bugs.php.net/bug.php?id = 48781

Based on my reading PHP has a lot reason to leak memory... like this https://bugs.php.net/bug.php?id=48781

有一个名为valgrind的工具可以提供帮助,请尝试一下:)

There is a tool called valgrind it can help, gone to try it :)

推荐答案

尽管unset()不会释放PHP进程消耗的内存,但会释放它供PHP脚本本身使用.

Although unset() does not free the memory consumed by the PHP process, it does free it for use by the PHP script itself.

因此,如果要在循环中创建大小为10M的10M变量,并在循环结束时取消设置(或重写)该变量,则内存消耗应低至10M +到最后所有其他变量循环.

So, if you are creating a variable of 10M of size 10 times in a loop and unsetting (or rewriting) it at the end of the loop, the memory consumption should be as low as 10M + all other variables by the end of the loop.

如果增长-那么,您不想显示的完整逻辑中的某处就会有泄漏.

If it grows - then, there is a leak somewhere in the full logic you don't want to show.

这篇关于PHP为什么未设置不会使内存释放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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