Last few week i worked on data scrapping projects, million of pages need to scrap and later insertion into database. cron jobs were set to process these records and were running for many days. memory management become critical for that project.
In PHP we have two most famous ways to free memory after job is done, set our variables to NULL or free using unset($var). i was unaware of any other way to free memory. i got problems when we run crons, memory goes higher after each iteration on Server and i was unable to control.
Another amazing behavior i observed and i want to share, that in Windows(on my localhost) when program run, program memory goes higher but at the end of iteration due to unset of variable and objects, it return to previous position. I was using memory_get_usage() to get stat and echoed at each iteration. but Linux server was doing different job memory never returned back and get higher after each iteration. even setting ini memory limit to -1 did no trick.
At the end we were thinking of purchasing more RAM to run these Cron jobs successfully. but that time these few lines did the trick I never thought.
//Memory cleanup for long-running scripts.
gc_enable(); // Enable Garbage Collector
var_dump(gc_enabled()); // true
var_dump(gc_collect_cycles()); // # of elements cleaned up
gc_disable(); // Disable Garbage Collector
This new Garbage Collection (also known as GC) mechanism is part of PHP 5.3 and can do amazing job when you are on long running script. gc_collect_cycles() will give you count for garbage cleanups. This script will not get you down due to memory shorting. Happy coding!