php garbage collection while script running

22,873

Solution 1

The key is that you unset your global variables as soon as you don't need them.

You needn't call unset explicitly for local variables and object properties because these are destroyed when the function goes out of scope or the object is destroyed.

PHP keeps a reference count for all variables and destroys them (in most conditions) as soon as this reference count goes to zero. Objects have one internal reference count and the variables themselves (the object references) each have one reference count. When all the object references have been destroyed because their reference coutns have hit 0, the object itself will be destroyed. Example:

$a = new stdclass; //$a zval refcount 1, object refcount 1
$b = $a;           //$a/$b zval refcount 2, object refcount 1
//this forces the zval separation because $b isn't part of the reference set:
$c = &$a;          //$a/$c zval refcount 2 (isref), $b 1, object refcount 2
unset($c);         //$a zval refcount 1, $b 1, object refcount 2
unset($a);         //$b refcount 1, object refcount 1
unset($b);         //everything is destroyed

But consider the following scenario:

class A {
    public $b;
}
class B {
    public $a;
}

$a = new A;
$b = new B;
$a->b = $b;
$b->a = $a;
unset($a); //cannot destroy object $a because $b still references it
unset($b); //cannot destroy object $b because $a still references it

These cyclic references are where PHP 5.3's garbage collector kicks in. You can explicitly invoke the garbage collector with gc_collect_cycles.

See also Reference Counting Basics and Collecting Cycles in the manual.

Solution 2

PHP garbage collection is largely a reference counter (it does have some cycle detection.) If you are keeping references which are still accessible around these will easily add up if not freed.

Use unset() to free variables you are no longer using. If you simply overwrite variables (eg, with null) this will only allow the GC to reduce to the amount of space required by that variable, but not as much as unset which actually allows the destruction of the referenced value.

You should also properly release any resources etc. that you use.

You will still see memory increase during runtime as the GC is free to release it at its own discression, such as when there are free cpu cycles or when it starts to run low on memory.

Solution 3

Use unset() as much as possible, check used memory more often. yes, php does garbage collection during runtime on a few conditions. here is a helpful post on php.net.

Solution 4

If the memory is increasing that much, then you are probably not releasing it. You have created a memory leak. Garbage collection won't help you if you don't unset variables, destroy objects and/or they go out of scope.

Are you unsetting the nodes you load once you are done with them? I've written PHP scripts that run for hours, processing millions of database records, with no problems and memory usage that goes up and down within a very acceptable range.

Share:
22,873
Icode4food
Author by

Icode4food

Updated on February 21, 2020

Comments

  • Icode4food
    Icode4food over 4 years

    I have a PHP script that runs on cron that can take up to an 15 minutes to execute. At regular intervals I have it spitting out memory_get_usage() so I can see what is happening. The first time it tells me my usage I am at 10 megs. When the script finishes I am at 114 megs!

    Does PHP do it's garbage collection while the script is running? Or what is happening to all that memory? Is there something I can do to force garbage collection. The task that my script is doing is a nightly import of a couple thousand nodes into Drupal. So it is doing the same thing a lot of times.

    Any suggestions?

  • Murad Sofiyev
    Murad Sofiyev over 6 years
    You write cannot destroy object $a because $b still references it but why when i write destruct class A this destruct execute when unset $a