Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exessive object memory usage when not explicitly unsetting

A colleague of mine wrote a script that was exhausting the available memory. I narrowed it down to the following basic test case:

for ( $i = 0; $i <= 20; $i ++ ) {
    echo memory_get_usage(). '<br />';
    $Survey = new Survey( 14 );
    echo memory_get_usage(). '<br /><br />';
}
exit('done');

This breaks on the third iteration:

3116696
49123440

49123440
95518368

95518368
[E_ERROR] Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes)

I managed to fix this, by simply adding an unset() call in the loop:

for ( $i = 0; $i <= 20; $i ++ ) {
    echo memory_get_usage(). '<br />';
    $Survey = new Survey( 14 );
    unset( $Survey );
    echo memory_get_usage(). '<br /><br />';
}
exit('done');

Now the script goes through its 20 iterations nice and smooth, with a relatively constant memory usage:

3116816
49123488

49123488
50691656

50691656
51088912

51088912
51079064

51079064
50535368

50535368
50809296

50809296
51033392

51033392
51157208

51157208
50543856

50543856
50892760

50892760
51045160

51045160
51132688

51132688
50535968

50535968
50968632

50968632
51058080

51058080
51143304

51143304
50562136

50562136
51067432

51067432
51067768

51067768
51170824

51170824
50551712

done

This confuses me! Isn't the garbage collector supposed to clean up the object, as it's variable has been overwritten? I'm running PHP 5.3, so circular references can't be the cause of this problem.

like image 876
Rijk Avatar asked Oct 25 '11 13:10

Rijk


People also ask

What causes high memory usage?

All computer memory is connected to the CPU and RAM. However, the high memory usage problem is mainly due to the overcrowding of many internal processes. Therefore, it helps to stop the unnecessary programs and applications that are running. Open the Task Manager and check any extra programs you aren't using.

Which of the following may cause a memory leak?

Common causes for these memory leaks are: Excessive session objects. Insertion without deletion into Collection objects. Unbounded caches.


1 Answers

Circular references can still be a problem in 5.3:

Cleanup Problems

Although there is no longer a symbol in any scope pointing to this structure, it cannot be cleaned up because the array element "1" still points to this same array. Because there is no external symbol pointing to it, there is no way for a user to clean up this structure; thus you get a memory leak. Fortunately, PHP will clean up this data structure at the end of the request, but before then, this is taking up valuable space in memory. This situation happens often if you're implementing parsing algorithms or other things where you have a child point back at a "parent" element. The same situation can also happen with objects of course, where it actually is more likely to occur, as objects are always implicitly used by reference.

There is probably also some memory-hogging resource inside Survey that takes up all of this memory; the observed behavior should be a combination of a ref cycle and such a resource.

What's in Survey exactly?

like image 133
Jon Avatar answered Oct 13 '22 02:10

Jon