How do I disable garbage collection for a long running php cli script? I am handling unsetting of variables in the script.
A quick workaround is to disable the System. gc () and force a Full GC. The –XX:+DisableExplicitGC command can be passed as a java parameter that disables System. gc() for the JVM.
You can disable GC and control when the GC has to be called in the runtime, by setting the environmental variable 'GOGC'. eg: GOGC=off disables the garbage collection.
When a JVM runs out of space in the storage heap and is unable to allocate any more objects (an allocation failure), a garbage collection is triggered. The Garbage Collector cleans up objects in the storage heap that are no longer being referenced by applications and frees some of the space.
The -Xdisableexplicitgc parameter allows the JVM to ignore these garbage collection suggestions. Typically, system administrators use this parameter in applications that show some benefit from its use. By default, calls to System. gc() trigger a garbage collection. Parent topic: Garbage Collector command-line options.
Unsetting variables does not free memory! It just removes the reference from the variable to the corresponding value. Once any value have a ref-count of 0
the GC collects the value and frees its allocated memory. If it would be possible to completely disable the GC you would break your interpreter (in best case): You will have many many unreferenced data in your memory, that will never get cleaned. Thats called "memory leak".
Is there garbage collection in PHP?
In my case the issue was related with the Zend Server running as a cli -- It crashed often and randomly. On the internet I found about disabling the 'garbage collector'
To do so,
in php.ini
zend.enable_gc = Off
also, it can be changed directly in the cli
$ php -d zend.enable_gc=0 your_script.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With