Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:31540 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 94483 invoked by uid 1010); 12 Aug 2007 19:57:43 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 94467 invoked from network); 12 Aug 2007 19:57:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Aug 2007 19:57:43 -0000 Authentication-Results: pb1.pair.com header.from=info@adaniels.nl; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=info@adaniels.nl; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain adaniels.nl from 82.94.236.173 cause and error) X-PHP-List-Original-Sender: info@adaniels.nl X-Host-Fingerprint: 82.94.236.173 loco.helderhosting.nl Linux 2.5 (sometimes 2.4) (4) Received: from [82.94.236.173] ([82.94.236.173:51807] helo=loco.helderhosting.nl) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9D/83-31622-6366FB64 for ; Sun, 12 Aug 2007 15:57:42 -0400 Received: from [192.168.2.13] (bas3-toronto01-1177780003.dsl.bell.ca [70.51.127.35]) by loco (Postfix) with ESMTP id C52EA11D055F; Sun, 12 Aug 2007 21:57:38 +0200 (CEST) Message-ID: <46BF664C.40506@adaniels.nl> Date: Sun, 12 Aug 2007 15:58:04 -0400 User-Agent: Thunderbird 1.5.0.12 (X11/20070604) MIME-Version: 1.0 To: ceo@l-i-e.com Cc: "Nathanael D. Noblet" , internals@lists.php.net References: <46BCEE3D.7080103@gnat.ca> <55685.209.254.223.2.1186897357.squirrel@www.l-i-e.com> In-Reply-To: <55685.209.254.223.2.1186897357.squirrel@www.l-i-e.com> Content-Type: multipart/alternative; boundary="------------020607040302030107090700" Subject: Re: [PHP-DEV] Pre-Destructor magic function? From: info@adaniels.nl (Arnold Daniels) --------------020607040302030107090700 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi Richard, class A { public $bs = array(); function addB() { $this->bs[] = new B($this); } } class B { public $parent; function __construct($parent) { $this->parent = $parent; } } function doSomething() { $a = new A(); $a->addB(); $a->addB(); } See the problem? The object $a isn't available from global space, but because of the circular reference, A and all its Bs won't be picked up by the garbage collector. Have this 22 000 times and you run out of memory. Best regards, Arnold Richard Lynch wrote: > On Fri, August 10, 2007 6:01 pm, Nathanael D. Noblet wrote: > > I'm not a PHP OOP expert, so this could be 100% wrong, but... > > >> $obj = new A(); in a loop the object's never released from memory >> because of the internal references it holds. __destruct is not called >> until the end of script execution etc. Now normally in a web request >> > > If __destruct isn't being called until the end of the script, then PHP > thinks you still have some way of accessing that object, and it can't > free it. > > >> situation this isn't a big deal. However I'm using it in a batch and >> loop over 22 000 records. Since re-assigning the single object I use >> isn't freeing any memory it consumes quite a bit. >> > > Perhaps set some things to NULL or unset them explicitly to convince > PHP that you are done with them. > > >> I have gotten around >> it by creating a special function in the object to manually release >> the >> recursive references. >> > > Well, there you go. > > Release the references so PHP knows you are done with them. > > >> However it would be nice if there were some sort >> of 'magic' function that could be called on an object so that it knows >> it is being deleted, sort of like a __preDestruct or something like >> that... Is that a bad idea? Or I guess better able to handle recursive >> references in objects would be the other way to solve it I guess. >> > > __destruct is called right before PHP destroys the object. > > There really isn't any event of any significance for a __perDesctruct... > > Either you're done with it, or you're not... > > --------------020607040302030107090700--