Greetings,
I am running a script with a very large loop, but the memory keeps hitting the limit. I add items to an array and after every 10,000 items or so, I would like to clear the array and free up memory, sort of like this:
echo memory_get_peak_usage()."<br>".memory_get_usage()."<br />";
$sql = "SELECT description FROM items LIMIT 300000";
$result = mysql_query($sql) or die(mysql_error());
$count = 0;
while($row = mysql_fetch_array($result)){
$count++;
$inserts[] = $row['description'];
if ($count % 10000 == 0) {
// ... do stuff with array ...
$inserts = ''; unset($inserts); // delete array - try to free up memory for next 10,000 items...
gc_enable();
var_dump(gc_enabled()); // true
var_dump(gc_collect_cycles()); // # of elements cleaned up - keeps showing up as "0".
$inserts = array();
echo memory_get_peak_usage()."<br>".memory_get_usage()."<br />";
}
}
I’ve been researching ways to try freeing the memory during the execution of a long script of loops. The “unset($variable);” and $variable = array(); don’t seem to work. I also tried using: gc_collect_cycles() , and this doesn’t work either. My PHP version is 5.4, so it should be working. My script says the garbage collector is enabled, but it’s not collecting anything. As you can see in the script, I print out the memory_get_usage() function and the memory just keeps growing and growing…
Please let me know how to get this to work so I can free up the memory and prevent hitting the limit.
Thanks
Kind regards