title: PHP failing with Out of Memory: Kill process - is it possible for me to make loops more efficient?
body: <p>My php is running out of memory with a server error "Out of memory:Kill process..about 25% of the way through the process"  Although it searches through about 10,000 lines, the number of lines that match the criteria, and therefore need to be stored and written to the file at the end of the process, are less than 200.  So I am not sure why it is running out of memory.</p>
  
  <p>Am I receiving this error because I am not clearing variables after each loop, or do I need to increase the memory on the server?</p>
  
  <p>The process in brief is:
   - LOOPA - loop through list of 400 zip codes
   - using one api call for each zip - get list of all places within each zip (typically about 40-50)</p>
  
  <p>-- SUBLOOP1 - for each place found, use an api call to get all events for that place</p>
  
  <p>---- SUBLOOP1A loop through events to count the number for each place</p>
  
  <pre><code>zips = file($configFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  $dnis = file($dniFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  $s3-&gt;registerStreamWrapper();
  $file = fopen("s3://{$bucket}/{$key}", 'w') or die("Unable to open file!");
  fwrite($file, $type . " id" . "\t" . $type . " name" . "\t" . "zip" . "\t" . "event count" . "\n" );
  
  foreach($zips as $n =&gt; $zip){
      //first line is the lable to describe zips, so skip it
      if ($n &lt; 1) continue;
      $params = $url;
      $params .= "&amp;q=" . $zip;
  
      $more_node_pages = true;
      while ($more_node_pages){
      $res = fetchEvents($params);
  
          //Now find the number of events for each place
          foreach($res-&gt;data as $node){
              //first check if on Do Not Include list
              $countevents = true;
              foreach($dnis as $dni) {
                  if ($dni == $node-&gt;id) {
                      echo "Not going to get events for ". $node-&gt;name . "   id# " . $dni . "\n\n";
                      $countevents = false;
                      break;
                  }
              }
              //if it found a match, skip this and go to the next
              if (!$countevents) continue;
              $params = $url . $node-&gt;id . "/events/?fields=start_time.order(reverse_chronological)&amp;limit=" . $limit . "&amp;access_token=". $access_token; 
  
              //Count the number of valid upcoming events for that node
              $event_count = 0;
              $more_pages = true;
              $more_events = true;
  
              while ($more_pages) {
  
                  $evResponse = fetchEvents($params);
                  if (!empty($evResponse-&gt;error)) {
                      checkError($evResponse-&gt;error-&gt;message, $evResponse-&gt;error-&gt;code, $file);
                  } 
  
                  //if it finds any events for that place, go throught each event for that place one by one to count until you reach today
                  foreach($evResponse-&gt;data as $event){
                      if(strtotime($event-&gt;start_time) &gt; strtotime('now')){
                          $event_count++;
                      }
                      //else we have reached today's events for this node, so get out of this loop, and don't retrieve any more events for this node
                      else {
                          $more_events = false;
                          break;
                      }
                  }
  
                  if (!empty($evResponse-&gt;paging-&gt;next) and $more_events) $params = $evResponse-&gt;paging-&gt;next;
                  else $more_pages = false;
              } //end while loop looking for more pages with more events for that node (page)
  
              if ($event_count &gt; "0") {
                  fwrite($file, $node-&gt;id . "\t" . $node-&gt;name . "\t" . $zip . "\t" . $event_count . "\n");
                  echo $event_count . "\n";
              }
          } // loop back to the next place until done
          //test to see if there is an additional page
          if (!empty($res-&gt;paging-&gt;next)) $params = $res-&gt;paging-&gt;next; else $more_node_pages = false;
      } //close while loop for $more_node_pages containing additional nodes for that zip
  } // loop back to the next zip until done
  fclose($file);
  </code></pre>