title: Long running loop slows down?
body: <p>I have a generator function in python which reads a dataset in chunks and yields each chunk in a loop. 
  On each iteration of the loop, the chunk size is the same and the data array is overwritten.</p>
  
  <p>It starts off by yielding chunks every ~0.3s and this slows down to every ~3s by about the 70th iteration. 
  Here is the generator:</p>
  
  <pre><code>def yield_chunks(self):
    # Loop over the chunks
    for j in range(self.ny_chunks):
      for i in range(self.nx_chunks):         
        dataset_no = 0
        arr = numpy.zeros([self.chunk_size_y, self.chunk_size_x, nInputs], numpy.dtype(numpy.int32))
  
        # Loop over the datasets we will read into a single 'chunk'
        for peril in datasets.dataset_cache.iterkeys():
          group = datasets.getDatasetGroup(peril)
          for return_period, dataset in group:
            dataset_no += 1
  
            # Compute the window of the dataset that falls into this chunk
            dataset_xoff, dataset_yoff, dataset_xsize, dataset_ysize = self.chunk_params(i, j)
  
            # Read the data
            data = dataset[0].ReadAsArray(dataset_xoff, dataset_yoff, dataset_xsize, dataset_ysize)
  
            # Compute the window of our chunk array that this data fits into
            chunk_xoff, chunk_yoff = self.window_params(dataset_xoff, dataset_yoff, dataset_xsize, dataset_ysize)
  
            # Add the data to the chunk array
            arr[chunk_yoff:(dataset_ysize+chunk_yoff), chunk_xoff:(dataset_xsize+chunk_xoff), dataset_no] = data
  
        # Once we have added data from all datasets to the chunk array, yield it
        yield arr
  </code></pre>
  
  <p>Is it possible that memory is not being properly released after each chunk, and this is causing the loop to slow down? Any other reasons?   </p>