4

I'm plotting some large plots in IPython QtConsole (and Notebook). These take up a lot of memory, but once they are plotted I don't need them any more and they can go.

How can I free up memory?

None of the following works:

close()
clf()
cla()
%reset

The only thing that frees the memory is restarting the kernel, which I don't always want to do (say I worked through a long process to get to a specific point) [To be precise, %reset does free up some memory, but not as much as restarting the kernel].

How to replicate the problem:

plot(arange(2e7))

You may need a bigger or a smaller number to notice a big impact on your system memory.

gozzilli
  • 8,089
  • 11
  • 56
  • 87
  • make sure nothing is holding references to the artist objects returned by `plot`. – tacaswell Jan 15 '15 at 16:33
  • I'm testing by literally just running the line `plot(arange(2e7))` in a pylab console (`ipython --pylab=inline`, for example), no more. No references (that I know of). – gozzilli Jan 15 '15 at 16:46
  • `plot` returns artists, ipython is probably capturing the in the output section. I assume you see something like `Out [N]: line2D object`? Also, please stop using pylab. – tacaswell Jan 15 '15 at 17:26
  • Yes, capturing the artist in a variable, deleting it and collecting garbage does help. `x = plot(arange(2e7)); del x; gc.collect()` after `import gc`. Thanks. – gozzilli Jan 15 '15 at 17:49

1 Answers1

3

After reading tcaswell's comment, I investigated a bit more what IPython is storing. This is my method:

## start afresh

## what variables are there before calling plot()?
## make sure you copy() otherwise it's just a reference.
before = vars().copy()

plt.plot(range(10))

## now, what's available after?
after = vars().copy()

## print out the differences
for k in after.keys():
  if k not in before.keys():
    print k

## or a bit more compact
print set(after.keys()) - set(before.keys())

Result is:

before
_i2
_i3
_2

Where _i* are string of the inputs and _n (with n a number) are the strings of the outputs. Removing those items and garbage collecting does not seem to free up memory though.

gozzilli
  • 8,089
  • 11
  • 56
  • 87