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)
2
  • 1
    Are you calling memory_get_usage() with or without a TRUE argument? It makes quite a difference: using the TRUE argument returns the actual memory requested using emalloc; while a FALSE argument shows the blocks of memory used Commented Jul 23, 2013 at 17:20
  • Rather than using PHP's own memory_get_usage() function to check memory usage, try using xhprof: the GUI makes it particularly easy to use Commented Jul 23, 2013 at 17:43

1 Answer 1

4

PHP has a garbage collector. It will free memory for variable containers which reference count is set to 0, meaning that no userland reference exists anymore.

I guess there are still references to variables which you might think to have cleaned. Need to see your code to show you what is the problem.

Sign up to request clarification or add additional context in comments.

19 Comments

PHP seems to collect garbage just fine, as I can see from the get_memory_usage() output. I know that these objects arent being referenced anymore since a lot of them are created and destroyed inside of a function any way.
You say that the garbage collector is buggy? Use debug_zval_dump($object) to get sure that there are no references.
I didnt say it was buggy :p I dont know if anything is buggy at all ,and Im just observing expected behaviour. Ill try that call.
@hek2mgl wouldn't you need a refernce to pass it to the function :)
@Orangepill :) yes, of course. I just meant directly before unset() the refcount should be 1 in that test. (was too lazy to explain that, thx for asking)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.