2

I have a script which uses a loop to generate data from a set of arrays:

   def mapping_func(QDdrive):
        start = tfunc()

        # this takes some input parameter and builds an array
        HCavQD = CavSBHam(QDDet, QDdrive, g_coup, CavDet, N)

        # this builds another array
        liou = diss(HCavQD, szCavQD, alp, wc, beta, gamma, kappa, N)

        # this builds a third and final array 
        st = steadystate(HCavQD, [liou], method = 'direct')

        # from the above array find these numbers
        g1QD = (spCavQD * smCavQD * st).tr()
        g1Cav = (adCavQD * aCavQD * st).tr()

        gcohQD = (spCavQD * st).tr() * (smCavQD * st).tr()
        gcohCav = (adCavQD * st).tr() * (aCavQD * st).tr()
        end = tfunc()
        print 'it took me %r seconds to do that calculation' % (end-start)

        #remove variables above
        del liou, HCavQD, st
        collect()
        return [g1QD, gcohQD, g1Cav, gcohCav]
# map above function over the list drives  
glist = [mapping_func(d) for d in drives]

After each loop the memory usage of the programme increases massively, even though I have used del on all the appropriate variables used. Naively I would have thought that the only thing being stored to memory are the four numbers returned by the function. Does anybody know why this might be/give some insight into the problem?

I have tried using a for loop rather than map with a similar outcome.

Thanks in advance,

J

Jiles
  • 199
  • 11
  • Is that `collect` method from the `gc` module? – Kevin Jan 30 '14 at 16:19
  • Yes, I was playing around to see if it solved the problem, no cigar unfortunately! – Jiles Jan 30 '14 at 17:24
  • This may be relevant: http://stackoverflow.com/questions/18310668 – Emilio Silva Jan 30 '14 at 23:11
  • Have you checked if the individual `drives` individually take less memory and _only when you put that in a for loop_ you have an increase in memory consumption? Theoretically, you shouldnt even have to delete the arrays that are created within the scope of a function. – ssm Jan 31 '14 at 01:05
  • Are those functions that generate arrays yours? Would it be possible to rewrite them to work with iterators rather than arrays or lists? That should cut down memory usage quite a lot. – aquavitae Jan 31 '14 at 07:53

0 Answers0