473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

efficient Python object count

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
6 2625
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
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
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
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
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
Here you are:http://svn.python.org/projects/pytho...5-maint/Misc/S...

Excellent, thank you.
Nov 7 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mikko Ohtamaa | last post by:
Hi, I am quite new to Python, PythonCom and COM generally. As a former Java programmer, I have found Python's flexible ability to access native Win32, especially COM, very comfortable. However,...
8
by: Robert Oschler | last post by:
Has anybody seen a Python module that will take an XML document (not a colossal one), and convert it to a Python nested class object? I'm basically looking for something that would allow me to...
3
by: Alexander Fillips | last post by:
Hi, my short question: is there a python object which can interpret java-script? the whole story ;-) I wrote some streaming-scripts for the xbox mediaplayer which supports python. for a...
5
by: Ara.T.Howard | last post by:
anyone out there know where i might find a python object model diagram? cheers. -a -- =============================================================================== | email :: ara t ...
3
by: Ron Guerin | last post by:
The New York Linux User's Group invites you to a special presentation by Alex Martelli of Google, on the Python Object Model. This presentation will be held at P.J. Clarke's Sidecar, rather than...
0
by: Ron Guerin | last post by:
(date and time inadvertently omitted last time. sorry!) The New York Linux User's Group invites you to a special presentation by Alex Martelli of Google, on the Python Object Model. This...
5
by: lg2779 | last post by:
Hello all I am trying to call the method of python object. But I dont know how to transfer the pointer of the python object into c++ . the C++ method to receive python object pointer : ...
1
by: AngelBlaZe | last post by:
Can you access the c object of a python object directly? Can this be done in ctypes or directly throught python functions without reverting to forking the python module? scanario: I have pyctx...
0
varuns
by: varuns | last post by:
hi i m confused in between python object and a c object (which is binded with python code using wrapper function) when i create a python object and use it to call a c api binded to python does...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.