Memory leaks/growth in Blender Python

Hi all,

I have serious memory usage growth in Blender python. I am trying to use a standard python profiler. However I am unable to get any profiler to work with Blender python.

Here is the test script that works with python 2.4.3 outside Blender:

#--------
import profile

def func1(N):
for i in range(N):
j=ii
k=3
i+j/4.0

profile.run(‘func1(1000000)’)
#--------

& returns:

     5 function calls in 1.140 CPU seconds

Ordered by: standard name

ncalls tottime percall cumtime percall filename:lineno(function)
1 0.050 0.050 0.050 0.050 :0(range)
1 0.000 0.000 0.000 0.000 :0(setprofile)
1 0.000 0.000 1.140 1.140 <string>:1(?)
1 1.090 1.090 1.140 1.140 TestProfiler.py:3(func1)
1 0.000 0.000 1.140 1.140 profile:0(func1(1000000))
0 0.000 0.000 profile:0(profiler)

If I run the same script in the Blender 2.46 ‘Interactive Python Console’, I get the following error:

File “TestProfiler.py”, line 9, in ?
profile.run(‘func1(1000000)’)
File “/usr/lib/python2.4/profile.py”, line 72, in run
prof = prof.run(statement)
File “/usr/lib/python2.4/profile.py”, line 448, in run
return self.runctx(cmd, dict, dict)
File “/usr/lib/python2.4/profile.py”, line 452, in runctx
sys.setprofile(self.dispatcher)
File “/usr/lib/python2.4/site-packages/psyco/profiler.py”, line 356, in psyco_setprofile
return result
File “/usr/lib/python2.4/profile.py”, line 238, in trace_dispatch_i
if self.dispatch[event](self, frame, t):
File “/usr/lib/python2.4/profile.py”, line 322, in trace_dispatch_return
assert frame is self.cur[-2].f_back, (“Bad return”, self.cur[-3])
AssertionError: (‘Bad return’, (‘profile’, 0, ‘func1(1000000)’))

Does anyone have any experience with using profilers in Blender python? Is there a solution or work around?

Thanks.

albcem

As an alternative to the above, does anyone have any tip on how to identify what function uses how much memory in a python program?

As a last alternative, someone may have some tips for suppressing memory growth in Blender python.

Thanks.

albcem

Your script should look like http://pasteall.org/2466/python to run in Blender. It runs fine for me.

Looking at your output it looks like you do other things too in your profile script, like running psyco etc.

/Nathan

Hello Nathan,

You were indeed right. It turns out psyco was causing the problem in running a profiler as well as the source of the memory growth. The moment I turned it off, my memory usage was as flat as the calm sea.

For future reference for the readers if you will use psyco to accelerate your code do not run psyco.full() on the whole program, but rather psyco.bind([function name]) on specific simple functions called often. Then you will have as fast or may be faster code than the psyco.full() case & your memory usage will be in control…

albcem