>This is not the same at all. When are you going to run this code? Memory >allocations happen all the time. What Nathan asked for is an event that is >triggered when the memory consumption reaches a >threshold. > >However, there is a different solution, which is better IMHO in the case of >caches: weak references. A weak reference automatically frees the memory of >the object, when the memory is needed. >http://php.net/manual/en/book.weakref.php. > >Having said that, none of these solutions scale up to multiple servers. >This is why shared cache systems like memcached are recommended.
I agree this probably is a good solution and I personally do use it along with shared memory tools, however there may be cases where the dev may gain more benefit from having a memory-warning installable trigger in place. This would allow things like allowing the dev to release certain cache objects before others or something completely different that I have not thought of yet. > Running the GC is most likely faster than most cleanup routines a user could > run, also usually there is not that much stuff cached in PHP scripts. If a > PHP script has "tons" of data, which it can easily throw >away, in memory > this sounds like a smell of an bad architecture. Cache cache-worthy stuff in > memcache or such and fetch only the data you need. > >Also: What should happen if the system runs out of memory while doing the >cleanup? Anything sane doesn't sound good either. Yes running the GC is much faster except they are two completely different processes... in my example the dev is keeping references to data for possible future use later on however it's not possible to know when to release these references so php's GC can collect them if the user does not implement something quite juristic like ticks or frequent function calls throughout a code base. >You can use ticks :) > >http://php.net/control-structures.declare#control-structures.declare.ticks Yes Ticks are something useable (like said above) however I have found ticks are clunky, frequently shunned, and you'd be ticking for no reason most of the time. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php