Why so many references to global variables?

J-P me at home.net
Thu Jul 3 15:38:43 EDT 2003


Hi,

I have a Python script interacting with a specialized
C numerical library. The whole program does quite a lot
of number crunching as should be running for a couple
of hours. However, it always seems to run out of memory
after maybe 40-45 minutes (I get a MemoryError from
the python interpreter). I wanted to see what took
that much memory so I printed a reference count
(using sys.getrefcount()) and I was suprised to
see that a global variable called 'myflag' has litteraly
millions of references to it. myflag appears maybe
8 or 10 times in the script, always something like

if myflag:
	#do something
else:
	#do something else

It appears that every time the interpreter tests for
the value of 'myflag', it keeps a reference to it.
I don't know whether this has something to do with
the garbage collector not doing its job correctly
of me doing something wrong in the code, but I'd really
like to fix this thing.

Any ideas, suggestions or comments greatly appreciated,
as always.

Thanks in advance,
J-P





More information about the Python-list mailing list