2

See this example:

echo memory_get_usage() . "\n"; // 36640
$a = str_repeat("Hello", 4242);
echo memory_get_usage() . "\n"; // 57960
unset($a);
echo memory_get_usage() . "\n"; // 36744

Can anyone explain why after un-setting the variable the memory usage does not return to 36640

1
  • No, if you are running script loop, it not always allocated max memory. Commented Mar 23, 2011 at 15:27

5 Answers 5

5

If you do it twice the memory will stay at 36744...

echo memory_get_usage() . "\n"; // 36640
$a = str_repeat("Hello", 4242);
echo memory_get_usage() . "\n"; // 57960
unset($a);
echo memory_get_usage() . "\n"; // 36744
$a = str_repeat("Hello", 4242);
unset($a);
echo memory_get_usage() . "\n"; // -> 36744
Sign up to request clarification or add additional context in comments.

4 Comments

It implies, that a long-running script is not "doomed"
I have the same question: How is this answer useful?
It shows you how the interpreter works. There is a small overhead even after unsetting something. But when doing it again there is no more memory overhead.
The first conclusion is obvious already from the original question. The second simply does not follow. And certainly after unsetting a variable that has already been unset we would expect nothing to change.
4

Garbage collection is an expensive operation, even if there's only a single variable to unset. PHP won't run the collector each time you unset a var, as that'd waste a huge amount of CPU time.

PHP will only run the collector when it has to, as in when something wants more memory than is available.

Comments

3

What is your PHP version? The garbage collector in versions less than 5.3 is not really good. Please read this link to understand why:

Garbage collector

4 Comments

He's freeing up the memory himself. Where does the GC come into it?
Try with 5.3 and post the results
Philippe: powtac has clarified that there is an overhead, but I am interested to see the difference between 5.2 and 5.3 and your response suggests you use 5.3 - Without having to install PHP on this PC, and without have to reinstall PHP on my server (which I am not prepaired to do today) can you enlighten us of the improvements between the old and new versions of the PHP GC by posting the results of this script?
I will favorite this thread and come back later because I can't right now.
1

Just posting this.

I just ran it as a test for fun on PHP 5.3, the results are pretty clear to what powtac said:

630744
652280
630808
630808
652280
630808
630808
652280
630808
630808
652280
630808
630808
652280
630808
630808
652280
630808

So yea, after the initial unset it appears to be consistent throughout. Code tested with:

while (1) {
        echo memory_get_usage() . "\n"; // 36640
        $a = str_repeat("Hello", 4242);
        echo memory_get_usage() . "\n"; // 57960
        unset($a);
        echo memory_get_usage() . "\n"; // 36744
}

Caution: that is an infinite loop :)

Comments

0

I'll try to give one possible explanation, but I cannot claim that it is the right one.

PHP stores variables in a hash table (because of it's dynamic nature). This hash table consists of "buckets" (linked lists of elements). As the number of elements grows the number of buckets is increased, too (to be precise: The number of buckets is doubled as soon as the limit is reached).

Thus it could be the case, that the creation of your variable results in an increase of buckets. As these buckets aren't removed again, the memory usage stays.

But again: Only guessing.

1 Comment

I would think the same thing: the 'test' is to define $a = '' first and run the memory_get_usage() function before assigning the huge string. The funny thing is, when I run the original code, my third result is exactly the same as my first.

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.