Python: How to reduce memory loss while compiling high numbers of images...
<p>I have been trying to create just shy of 2000 images using a netCDF file for Arctic Sea Ice concentrations. Just when I think it is working, I realize it is eating up so much memory that it either creeps along, or just fails. 
So far, I have tried clearing all the variables within my -for- loop, and I have tried to use the plt.close(fig) and plt.close('all'). None of which helped. </p>

<p>Reaching out now...Does anyone have any other suggestions I could try? 
I need to be able to save each image in the file. </p>

<pre><code>import netCDF4 as nc
import pandas as pd
import numpy as np
import datetime as datetime
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

## Read in netCDF file.  NOTE: Change format if it does not read.
NH_Ice=nc.Dataset('Oc-Tech/Final_Project/ncFiles/NH_SeaIce.nc', 'r')

## Save netCDF parameters into arrays.
lons = NH_Ice.variables['longitude'][:]
lats = NH_Ice.variables['latitude'][:]
time = NH_Ice.variables['time'][:]
time_units = NH_Ice.variables['time'].units

## Calculate Date from given data file
dbase= '2001-01-01 00:00:00'
db=datetime.datetime.strptime(dbase, "%Y-%m-%d %H:%M:%S")
start=db+datetime.timedelta(days=time[0])
end=db+datetime.timedelta(days=time[-1])

#print fh.time_strlen
si = NH_Ice.variables['seaice_conc'][:]
NH_Ice.close()

m = Basemap(projection='npstere',boundinglat=45,lon_0=-156,resolution='c')   

#total time steps
tts=1980
for ti in range(0,tts):

    fig = None
    lat = None
    lon = None
    xi = None
    yi = None
    sip = None

    fig=plt.figure(figsize=(10,10))
    m.drawcoastlines(color='black')
    m.fillcontinents(color='limegreen',lake_color='blue')
    m.drawparallels(np.arange(-80.,81.,20.)) 
    m.drawmeridians(np.arange(-180.,181.,20.))
    m.drawmapboundary(fill_color='darkblue')

    lon, lat = np.meshgrid(lons, lats) #sea ice
    xi, yi = m(lon, lat)

    sip=(si[ti])  ## Plot u data from a particular day

    m.pcolor(xi,yi,np.squeeze(sip))     
    plt.savefig("fig"+str(ti)+".png")
    #plt.close(fig)
</code></pre>