[SciPy-user] Debugging memory exhaustion in Python?

Robert Kern robert.kern@gmail....
Sat Jun 16 15:31:07 CDT 2007


Greg Novak wrote:
> Is there a simple way to get IPython to release its references?  I'm
> interested in that, too, independently.  Is it as simple as clearing
> out the In[] and Out[] lists?

There are also variables _NN which correspond to Out[NN] that need to be
deleted. There are also _, __, and ___, but those will get rotated shortly.
Also, don't worry about In; it's just the strings you typed, nothing too memory
consuming.

Here's a function that you can use:

import bisect

def clearout(__IP, upto=None):
    """ Clear the IPython Out cache, possibly only up to a given entry.
    """
    ns = __IP.ns_table['user']
    Out = ns.get('Out', None)
    if Out is not None:
        keys = sorted(Out)
        if upto is not None:
            keys = keys[:bisect.bisect_right(keys, upto)]
        for key in keys:
            del Out[key]
    else:
        # No cache.
        # Still might have the _NN variables sitting around.
        keys = []
        for var in ns:
            if var.startswith('_'):
                try:
                    nn = int(var[1:])
                except ValueError:
                    continue
                if upto is not None and nn < upto:
                    keys.append(nn)

    for key in keys:
        _key = '_%s' % key
        del ns[_key]

    print 'Remove Out entries: %s' % keys



In [1]: from clearout import clearout

In [2]: 2
Out[2]: 2

In [3]: 3
Out[3]: 3

In [4]: 4
Out[4]: 4

In [5]: 5
Out[5]: 5

In [6]: 6
Out[6]: 6

In [7]: 7
Out[7]: 7

In [8]: 8
Out[8]: 8

In [9]: 9
Out[9]: 9

In [10]: 10
Out[10]: 10

In [11]: print Out
{2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10}

In [12]: clearout(__IP, upto=6)
Remove Out entries: [2, 3, 4, 5, 6]

In [13]: print Out
{7: 7, 8: 8, 9: 9, 10: 10}


-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the SciPy-user mailing list