Releasing PHP memory using __destruct

Asked

Viewed 520 times

0

It is necessary to use the method __destruct and define the variables of the class with null in order to free up memory more efficiently?

For example:

class Teste {
    public $foo;
    public $bar;
    // ...

    public function __construct() {
        // ...    
    }

    public function __destruct() {
        $this->foo = null;   
        $this->bar = null;   
    }

}

2 answers

5

I don’t think so, after all __destruct will be called automatically when all references are released, or when the script ends.

Also, if you set as null does not release memory alone, it just marks as a candidate for garbage collection, which is exactly what the _destruct ago.

2


__destruct is automatically called at the end of the script as stated in the script itself documentation Destrutores são chamados durante o encerramento do script tendo os cabeçalhos HTTP enviados.

And an addendum: allocate null a variable still consumes memory because there is still a reference to the property that has value null.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.