By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,525 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,525 IT Pros & Developers. It's quick & easy.

efficient Python object count

P: n/a
Hello,

Does anyone know of an efficient way to get a count of the total
number of Python objects in CPython? The best solution I've been able
to find is len(gc.get_objects()) which unfortunately has to walk a C
linked list *and* creates a list containing all of the objects, when
all I need is an object count.

Thanks!
Nov 7 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
darrenr wrote:
Hello,

Does anyone know of an efficient way to get a count of the total
number of Python objects in CPython? The best solution I've been able
to find is len(gc.get_objects()) which unfortunately has to walk a C
linked list *and* creates a list containing all of the objects, when
all I need is an object count.
There is no way to get correct numbers with a standard Python compiler.
You have to use a special debug build but that requires debug builds of
all extensions too.
gc.get_object() lists only object that are maintained by gc. Several
types don't use gc, for example str, unicode, int, float, long and more.

Christian

Nov 7 '08 #2

P: n/a
On Nov 6, 7:28*pm, Christian Heimes <li...@cheimes.dewrote:
darrenr wrote:
Hello,
Does anyone know of an efficient way to get a count of the total
number of Python objects in CPython? The best solution I've been able
to find is len(gc.get_objects()) which unfortunately has to walk a C
linked list *and* creates a list containing all of the objects, when
all I need is an object count.

There is no way to get correct numbers with a standard Python compiler.
You have to use a special debug build but that requires debug builds of
all extensions too.
gc.get_object() lists only object that are maintained by gc. Several
types don't use gc, for example str, unicode, int, float, long and more.

Christian
Thanks for the quick reply. Could you provide a link to more
information on the debug build you refer to?
Nov 7 '08 #3

P: n/a
Thanks for the quick reply. Could you provide a link to more
information on the debug build you refer to?
A modified version of this algorithm should do the trick for my
purposes, it finds the non-containers that gc ignores. I don't care
how long it takes to compute, I just don't want the process to block
for long periods of time.

http://utcc.utoronto.ca/~cks/space/b.../GetAllObjects
Nov 7 '08 #4

P: n/a
darrenr wrote:
>Thanks for the quick reply. Could you provide a link to more
information on the debug build you refer to?

A modified version of this algorithm should do the trick for my
purposes, it finds the non-containers that gc ignores. I don't care
how long it takes to compute, I just don't want the process to block
for long periods of time.

http://utcc.utoronto.ca/~cks/space/b.../GetAllObjects
Once I had a strange memory leak in a long running process. This
turned out to be a problem of objects filing up in gc.garbage
("uncollectables").
I think those objects where also not reachable through
gc.get_objects() - as used by the script above. So be aware. Maybe
you've got the same motive for your counter.
I was somewhat stunn seeing, that obviously almost any serious
bigger app needs to watch and handle/free the gc.garbarge list "by
hand", to remain stable.
Robert
Nov 7 '08 #5

P: n/a
darrenr wrote:
Thanks for the quick reply. Could you provide a link to more
information on the debug build you refer to?
Here you are:
http://svn.python.org/projects/pytho...cialBuilds.txt

Nov 7 '08 #6

P: n/a
Here you are:http://svn.python.org/projects/pytho...5-maint/Misc/S...

Excellent, thank you.
Nov 7 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.