1

So, I am running a long-running script that is dealing with memory sensitive data (large amounts of it). I (think) I am doing a good job of properly destroying large objects throughout the long running process, to save memory.

I have a log that continuously outputs current memory usage (using memory_get_usage()), and I do not notice rises and drops (significant ones) in memory usage. Which tells me I am probably doing the right thing with memory management.

However, if I log on to the server and run a top command, I notice that the apache process that is dealing with this script never deallocates memory (at least visibly though the top command). It simply remains at the highest memory usage, even if the current memory usage reported by php is much, much lower.

So, my question is: are my attempts to save memory futile if the memory isnt really being freed back to the server? Or am I missing something here.

Thank you.

ps. using php 5.4 on linux

pps. For those who want code, this is a basic representation:

function bigData()
{
    $obj = new BigDataObj();
    $obj->loadALotOfData();

    $varA = $obj->getALotOfData();

    //all done
    $obj = NULL;
    $varA = NULL;
    unset($obj,$varA);
}

update: as hek2mgl recommended, I ran debug_zval_dump(), and the output, to me, seems correct.

function bigData()
{
    $obj = new BigDataObj();
    $obj->loadALotOfData();

    //all done
    $obj = NULL;

    debug_zval_dump($obj);

    unset($obj);

    debug_zval_dump($obj);
}

Output:

NULL refcount(2)

NULL refcount(1)
4

1 回答 1

3

PHP 有一个垃圾收集器。它将为引用计数设置为 的变量容器释放内存0,这意味着不再存在用户空间引用。

我想仍然有对您可能认为已经清除的变量的引用。需要查看您的代码以向您展示问题所在。

于 2013-07-23T17:18:50.287 回答