Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:54332 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 94807 invoked from network); 4 Aug 2011 06:03:53 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Aug 2011 06:03:53 -0000 Authentication-Results: pb1.pair.com smtp.mail=lars.schultz@toolpark.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=lars.schultz@toolpark.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain toolpark.com from 195.49.42.12 cause and error) X-PHP-List-Original-Sender: lars.schultz@toolpark.com X-Host-Fingerprint: 195.49.42.12 mail1.screenlight.ch Received: from [195.49.42.12] ([195.49.42.12:62073] helo=mail1.screenlight.ch) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8C/91-14872-7463A3E4 for ; Thu, 04 Aug 2011 02:03:52 -0400 Received: from [192.168.1.112] ([192.168.1.112]) (authenticated user lars.schultz@toolpark.com) by mail1.screenlight.ch (Kerio Connect 7.0.2 patch 1) (using TLSv1/SSLv3 with cipher AES256-SHA (256 bits)) for internals@lists.php.net; Thu, 4 Aug 2011 08:03:48 +0200 Message-ID: <4E3A3643.2070305@toolpark.com> Date: Thu, 04 Aug 2011 08:03:47 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.9) Gecko/20100915 Lightning/1.0b2 Thunderbird/3.1.4 MIME-Version: 1.0 To: internals@lists.php.net References: <4E3898B0.40809@sugarcrm.com> <4E38EC0C.9080304@lerdorf.com> <4E38FA2E.4030605@lsces.co.uk> <4E38FC67.9090200@toolpark.com> <4E39E89F.8060605@sugarcrm.com> In-Reply-To: <4E39E89F.8060605@sugarcrm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [VOTE] Weak References From: lars.schultz@toolpark.com (Lars Schultz) Am 04.08.2011 02:32, schrieb Stas Malyshev: > I'm not sure I understand why you need week refs there - can't you just > always use $prodDb->getProduct(1) and when you don't need it anymore > just do $prodDb->drop(1)? Or let it drop it whenever it wants to? My Project requires lots of DB-Record <=> PHP-Object mapping, as the objects get used quite dynamically I can't know when and how often an object will be used. To make sure there's only one instance of an Object in Memory per Session, I cache them in an associative-array, the key being the record-id (actually a PHP-Object consists of many records). Then I am using the Singleton-Pattern like Library::getObject($id) which returns a new (fetched freshly from the DB) or an already used object (from the cache). This works very well and keeps me from expensively reconstructing objects from the DB. What it does not work well with is Garbage collection, since the an object will always be referenced at least once (by the cache). The longer a script is running, the higher the chance that it will run out of memory (fast). I do want to keep those objects for as long as possible, but not if the price is an "out of memory" fatal error. My solution to this was neither nice nor correct, but I could not think of anything else: Whenever I'd create a fresh object I'd check memory against its limit and if it runs close, I will discard the oldest objects from the cache, always running the risk that my singleton is suddenly not so single anymore, because a discarded object was still referenced somewhere else. So either I get to know if the object is actually in use somewhere else than just the cache (using the zval-refcount method) or PHP provides me with something like WeakReference or even better: SoftReference, which would give me more control as to when it would be collected. I am open to any suggestions how I could solve my problem without WeakReference or zval-refcount (short of keeping a ref-count in userland).