mayavi icon indicating copy to clipboard operation
mayavi copied to clipboard

Memory leak while rendering many images

Open solarjoe opened this issue 8 years ago • 6 comments

Hello,

there might be a memory leak when rendering many images in a for loop.

WinPython-64bit-3.4.4.1 Mayavi 4.4.4 VTK 7.0.0

Running the code below I can see the "Commit" memory filling up. Once full it crashes the memory frees. The calculations started at the red marks, were stopped or crashed at the black marks. At the blue marks the "Physical memory" started to fill up.

grafik

I don't know exactly on how to interpret this.

Other things I noticed

  • the issue is not present when using mlab.options.offscreen=True
  • the window title states "Mayavi Scene #", this # being the number of the image rendered. Shouldn't this be reset to 1 since I am closing the figure after each run?
  • using gc.collect() does not free the memory
  • similar issue mentioned on stackoverflow
  • and one at sourceforge

Here is a working example:

#import gc

import numpy as np
from mayavi import mlab

#mlab.options.offscreen=True

n = 10000
nt = 5000

for k in range(10000):

    x = np.random.rand(n)
    y = np.random.rand(n)
    z = np.random.rand(n)

    triangles = np.random.randint(n, size=(nt, 3))

    fig = mlab.figure(bgcolor=(1.0, 1.0, 1.0), size=(1600, 900))

    mlab.triangular_mesh(x, y, z, triangles, scalars=np.random.rand(n))

    mlab.clf()
    mlab.close(fig)
    mlab.close(all=True)

    #gc.collect()

solarjoe avatar Jul 05 '17 06:07 solarjoe

@solarjoe, did you discover a workaround for this?

csbrown avatar Dec 03 '18 17:12 csbrown

No, sorry. Do you see a similar behaviour?

solarjoe avatar Jan 05 '19 08:01 solarjoe

@solarjoe Put the figure code outside the loop, and just clear the figure not close the window. Then you will find the magic happen. ` import numpy as np from mayavi import mlab

#mlab.options.offscreen=True

n = 10000 nt = 5000 fig = mlab.figure(bgcolor=(1.0, 1.0, 1.0), size=(1600, 900)) for k in range(10000):

x = np.random.rand(n)
y = np.random.rand(n)
z = np.random.rand(n)

triangles = np.random.randint(n, size=(nt, 3))



mlab.triangular_mesh(x, y, z, triangles, scalars=np.random.rand(n))

mlab.clf()

`

eleboss avatar Jun 27 '19 04:06 eleboss

@eleboss, that seems to work, thanks! Any idea why the garbage collection does not work?

solarjoe avatar Jun 27 '19 04:06 solarjoe

so far, nop. Best.

---Original--- From: "solarjoe"[email protected] Date: Thu, Jun 27, 2019 12:52 PM To: "enthought/mayavi"[email protected]; Cc: "eleboss"[email protected];"Mention"[email protected]; Subject: Re: [enthought/mayavi] Memory leak while rendering many images (#517)

@eleboss, that seems to work, thanks! Any idea why the garbage collection does not work?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

eleboss avatar Jun 27 '19 05:06 eleboss

Mess up while using multi-processing, may because single figure is created but using clf by different thread. :-/ Put the figure code inside the loop and so far so good. :->

@solarjoe Put the figure code outside the loop, and just clear the figure not close the window. Then you will find the magic happen. ` import numpy as np from mayavi import mlab

#mlab.options.offscreen=True

n = 10000 nt = 5000 fig = mlab.figure(bgcolor=(1.0, 1.0, 1.0), size=(1600, 900)) for k in range(10000):

x = np.random.rand(n)
y = np.random.rand(n)
z = np.random.rand(n)

triangles = np.random.randint(n, size=(nt, 3))



mlab.triangular_mesh(x, y, z, triangles, scalars=np.random.rand(n))

mlab.clf()

`

yougrianes avatar Dec 14 '22 13:12 yougrianes