Q:

PHP: How do I free limited scope variables?

I am getting "Fatal error: Allowed memory size of XXXX bytes exhausted...". I need to iterate through big amount of records, and execute a function to verify of the record fit the criteria which declare many class variables.
foreach ($results as $row)
{
  $location = Location::parseDatabaseRow($row);

  if ($location->contains($lat, $lon))
  {
    $found = true;
    $locations[] = $location;
    break;
  }
}

Implementation of the Location class:
public function contains($lat, $lon)
{ 
  $polygon =& new polygon();
  .... //Add points to polygons base on location polygons
  $vertex =& new vertex($lat, $lon);
  $isContain = $polygon->isInside($vertex); 

  $polygon->res(); //Reset all variable inside polygons
  $polygon = null; //Let Garbage Collector clear it whenever
  return ($isContain);
}

Shouldn't the $polygon be clear when contain() method is return? What can I do to reduce memory usage?
I am a Java developer, and started to learn PHP. Please help me understand on how to manage stack size and memory allocation and delocation. Thanks in advance.

A:

I am a Java developer, and started to learn PHP.

Here are some corrections which may allow your code to not exhaust memory limit.
use a while. Since your result comes from a database query, you should have the possibility to use fetch() instead of fetchAll() which I assume you are using since you are applying a foreach() on it.
while ($row = $result->fetch()) { // here $result is supposed to be a PDOStatatement.
    $location = Location::parseDatabaseRow($row);
    if ($location->contains($lat, $lon)) {
        $found = true; // where is this used?
        $locations[] = $location;
        break;
    }
}

While uses less memory because not all results are fetched at the same time.
use the ampersand the right way. You are doing a new in each loop. The ampersand is used when you want to pass a value by reference to a function, so that it is affected outside that function scope without the need to return it.
Here, you are using objects, which are somewhat passed by reference by design.
public function contains($lat, $lon) {
    $polygon = new polygon();
    $vertex = new vertex($lat, $lon);
    return $polygon->isInside($vertex);
    // no need to reset the values of your polygon, you will be creating a new one on the next loop.
}

for completeness sake here a version using the same polygon object. Notice how I do not use an ampersand because we are passing an object.
$polygon = new polygon();
while ($row = $result->fetch()) { // here $result is supposed to be a PDOStatatement.
    $location = Location::parseDatabaseRow($row);
    if ($location->contains($lat, $lon, $polygon)) {
        $found = true; // where is this used?
        $locations[] = $location;
        break;
    }
}

public function contains($lat, $lon, $polygon) {
    //Add points to the passed polygon
    $vertex = new vertex($lat, $lon);
    $isContain = $polygon->isInside($vertex);
    $polygon->res(); 
    // since we eill be using the same $polygon, now we need to reset it
    return $isContain;
}

