Why so many references to global variables?

J-P me at home.net
Fri Jul 4 01:19:21 CEST 2003


Alexander Schmolck wrote:
> J-P <me at home.net> writes:
> 
> 
>>It appears that every time the interpreter tests for
>>the value of 'myflag', it keeps a reference to it.
> 
> 
> What makes you think so? This seems rather unlikely to me (not that the
> references themselves should eat your memory anyway!). 

Well, sys.getrefcount() does tell me there are 3 millions  references
to it and other globals. Even though this doesn't eat up all my memory
(how large is a reference object in Python?), I definitely think there's 
something fishy with keeping that many references to global variables 
that appear here and there in the script.



> Chances are, the C extension code doesn't work correctly (C extensions to
> python code have to do memory management by hand; increasing and decreasing
> reference counts for the python objects they deal with as appropriate; so if a
> bit of code forgets to decrease the refcount, the object will stay alive
> forever; my guess would be that this it what happens here).

Might be, but the ref count for the objects interacting with the C 
library are pretty much what I expect them to be, i.e. a few dozens.
I don't think there are other memory leaks in the bindings to the
library. I've passed it through Purify a couple of times and everything
seems clean.


J-P





More information about the Python-list mailing list