gc_collect_cycles - not working

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

Did you try setting the variable to NULL instead of marking it for garbage collection with unset()?

http://stackoverflow.com/questions/7189328/how-to-free-memory-in-php

My understanding of memory_get_peak_usage() is that it will return the peak memory used by your script during execution. When I have used it, I always place it at the very end of the script. I do not know how you can determine the peak usage of a script until it has neared the end. I also set the real_usage parameter to true: memory_get_peak_usage(true).

By the way, how much memory are you consuming? When I think I may need a little more memory, I increase the memory limit beyond the default 128 MB on the shared server I use.

I tried setting the variables to “NULL” as well and this didn’t work. I also print out the memory usage using: “memory_get_usage()” and can see the current usage. Both this one and the peak usage figures continue to climb until my memory maxes out. I currently have 90MB of memory set on my shared hosting server.