php – How to force memory cleanup


It is required to unload about 50 thousand lines from the database into an Excel file. 1 GB of RAM is available. I load 10 thousand in a loop 5 times (it has been experimentally established that this is about 500MB of data) into the $ records variable. At the end of the loop I do unset ($ records);

On the 2nd cycle I have

PHP Fatal error:  Allowed memory size of 1073741824 bytes exhausted 

gc_enabled is on, and memory_get_usage shows that after the memory after each pass of the loop, it only grows by +500. GC doesn't want to work. How can I clear my memory? How can I load a large amount of data with low RAM capacities?


How can I clear my memory?

gc_collect_cycles – Force the garbage collector to run.

Explicitly launches the circular reference search engine.

Returns the number of links found.

Source: gc_collect_cycles ()

Scroll to Top