note 98719 modified in function.unset by danbrown

From: Date: Fri, 02 Jul 2010 14:53:51 +0000
Subject: note 98719 modified in function.unset by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170660@lists.php.net to get a copy of this message
further I realized that an object, when getting detroyed, does care about destroying variable in object space visibility but not those in local visibility, be aware of the found pattern: <?php class release_test{ private $buffer; private $other_object; public function __construct(){ $this->other_object=new other_object_class(); } public function __destruct(){ //note that you always have to unset class objects, in order to get the resources released unset($this->other_object); } public allocate_mem_A(){ $this->buffer=file("/tmp/bigfile"); } public allocate_mem_B(){ $buffer=file("/tmp/bigfile"); } public allocate_mem_C(){ $buffer=file("/tmp/bigfile"); unset($buffer); } public allocate_mem_D(){ $this->other_buffer=file("/tmp/bigfile"); } } //this does not lead to a resource problem $A=new release_test(); $A->allocate_mem_A(); $A->__destruct(); unset($A); //this DOES lead to a resource problem $B=new release_test(); $B->allocate_mem_B(); $B->__destruct(); unset($B); //this does not lead to a resource problem $C=new release_test(); $C->allocate_mem_C(); $C->__destruct(); unset($C); //this does not lead to a resource problem $D=new release_test(); $D->allocate_mem_D(); $D->__destruct(); unset($D); ?> --was-- further I realized that an object, when getting detroyed, does care about destroying variable in object space visibility but not those in local visibility, be aware of the found pattern: class release_test{ private $buffer; private $other_object; public function __construct(){ $this->other_object=new other_object_class(); } public function __destruct(){ //note that you always have to unset class objects, in order to get the resources released unset($this->other_object); } public allocate_mem_A(){ $this->buffer=file("/tmp/bigfile"); } public allocate_mem_B(){ $buffer=file("/tmp/bigfile"); } public allocate_mem_C(){ $buffer=file("/tmp/bigfile"); unset($buffer); } public allocate_mem_D(){ $this->other_buffer=file("/tmp/bigfile"); } } //this does not lead to a resource problem $A=new release_test(); $A->allocate_mem_A(); $A->__destruct(); unset($A); //this DOES lead to a resource problem $B=new release_test(); $B->allocate_mem_B(); $B->__destruct(); unset($B); //this does not lead to a resource problem $C=new release_test(); $C->allocate_mem_C(); $C->__destruct(); unset($C); //this does not lead to a resource problem $D=new release_test(); $D->allocate_mem_D(); $D->__destruct(); unset($D); http://php.net/manual/en/function.unset.php

« previous php.notes (#170660) next »