I’m doing some PHP memory benchmarks and i’m wondering if there is a way to optimize the garbage collector in order to reduce the memory consumption (as it is possible in other languages such as JAVA).
I’ve found only three customizable parameters related to GC in the php.ini: session.gc_probability, session.gc_divisor and session.gc_maxlifetime. This is only for sessions and i’m not benchmarking that.
Possible optimizations i know so far are code related, such as avoiding circular references and by forcing a garbage collecting cycle by calling gc_collect_cycles() (thanks to this article http://www.alexatnet.com/comment/86).
Someone know any configuration tricks or good practices in PHP memory management ?
So far as I know, forcing a collection with
gc_collect_cycles()is the closest you can get to minimising memory usage at time t.GC is of course a time-bound operation, and an application is rarely critically memory-bound, and as such it does not really make sense to offer options to unnecessarily trigger GC runs throughout the program’s execution. Along this vein, PHP does offer the ability to turn the cycle collector on and off on demand (
gc_enable(),gc_disable()) such that you can optimise your code for time (avoiding the overhead of the GC deciding to kick off) – of course it’s easy to see how this might be useful.In general, PHP by philosophy avoids topics such as memory management and GC, and your benchmarks should perhaps pay respect to this for a more real-world outlook.
Hope this helps.
(A nod to @GordonM’s comment citing micro-optimisation, also.)