473,796 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get memory size/usage of python object


Is there a way to check the REAL size in memory of a python object?

Something like
print sizeof(mylist)
or
print sizeof(myclass_ object)
or something like that ...

Thanks.
Jan 9 '08 #1
5 25783
Santiago Romero wrote:
Is there a way to check the REAL size in memory of a python object?
in standard Python, without reading the interpreter source code
carefully, no.

to get an approximate value, create a thousand (or a million) objects
and check how much the interpreter grows when you do that.

</F>

Jan 9 '08 #2
Santiago Romero <sr*****@gmail. comwrote:
Is there a way to check the REAL size in memory of a python object?

Something like
>print sizeof(mylist)
[ ... ]
Would you care to precisely define "REAL size" first? Consider:
>>atuple = (1, 2)
mylist = [(0, 0), atuple]
Should sizeof(mylist) include sizeof(atuple) ?
>>del atuple
What about now, when mylist has the only reference to the (1, 2)
object that also used to be referred to as atuple?

--
\S -- si***@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Jan 9 '08 #3
Sion Arrowsmith wrote:
Santiago Romero <sr*****@gmail. comwrote:
>Is there a way to check the REAL size in memory of a python object?

Something like
>>print sizeof(mylist)
[ ... ]

Would you care to precisely define "REAL size" first? Consider:
>>>atuple = (1, 2)
mylist = [(0, 0), atuple]

Should sizeof(mylist) include sizeof(atuple) ?
>>>del atuple

What about now, when mylist has the only reference to the (1, 2)
object that also used to be referred to as atuple?
or add to the mix
>>mylist = [(0,0), atuple] * 1000
where the same atuple is referenced 1000 times. And then if you
>>del atuple
defining "sizeof()" becomes even more peculiar if you have a
thousand things that "have" the same item that nothing else
claims ownership of.

Or, if you have this:
>>alist = [1,2,3]
mylist = ['a', 'b', alist] * 10
s1 = sizeof(mylist)
alist.append( 42)
s2 = sizeof(mylist)
should s1==s2 ?

-tkc
Jan 9 '08 #4
Would you care to precisely define "REAL size" first? Consider:
>atuple = (1, 2)
mylist = [(0, 0), atuple]

Should sizeof(mylist) include sizeof(atuple) ?
No, I'm talking about "simple" lists, without REFERENCES to another
objects into it.

I mean:

lists = [ 0, 1, 2, 3, 4, (1,2), 3]

or

array = [ [0,0,0,0,0,0,0], [1,1,1,1,2,1,2], ... ]

Maybe I can "pickle" the object to disk and see the filesize ... :-?

Jan 10 '08 #5
On Thu, 10 Jan 2008 00:14:42 -0800, Santiago Romero wrote:
>Would you care to precisely define "REAL size" first? Consider:
>>atuple = (1, 2)
mylist = [(0, 0), atuple]

Should sizeof(mylist) include sizeof(atuple) ?

No, I'm talking about "simple" lists, without REFERENCES to another
objects into it.

I mean:

lists = [ 0, 1, 2, 3, 4, (1,2), 3]
That list has 7 references to other objects. One of those objects has 2
references to objects.

In total, depending on implementation, there could be as many as 9
objects referenced by that list, or as few as 6 objects (both references
to 3 could be to the same object).

In the current CPython implementation, that list will have 7 references
to 6 objects. Including indirect references, there will be 9 references
to 6 objects. (Or so I understand.)

or

array = [ [0,0,0,0,0,0,0], [1,1,1,1,2,1,2], ... ]
Ignoring the '...', there will be a total of 16 references to 5 objects
in the current CPython implementation. Other Pythons (Jython, IronPython,
PyPy, ...) may be different.

Maybe I can "pickle" the object to disk and see the filesize ... :-?
That would measure something very different.

Possibly you want something like this heuristic:

def sizeof(obj):
"""APPROXIM ATE memory taken by some Python objects in
the current 32-bit CPython implementation.

Excludes the space used by items in containers; does not
take into account overhead of memory allocation from the
operating system, or over-allocation by lists and dicts.
"""
T = type(obj)
if T is int:
kind = "fixed"
container = False
size = 4
elif T is list or T is tuple:
kind = "variable"
container = True
size = 4*len(obj)
elif T is dict:
kind = "variable"
container = True
size = 144
if len(obj) 8:
size += 12*(len(obj)-8)
elif T is str:
kind = "variable"
container = False
size = len(obj) + 1
else:
raise TypeError("don' t know about this kind of object")
if kind == "fixed":
overhead = 8
else: # "variable"
overhead = 12
if container:
garbage_collect or = 8
else:
garbage_collect or = 0
malloc = 8 # in most cases
size = size + overhead + garbage_collect or + malloc
# Round to nearest multiple of 8 bytes
x = size % 8
if x != 0:
size += 8-x
size = (size + 8)
return size
See:
http://mail.python.org/pipermail/pyt...ch/135223.html

to get you started.

--
Steven.
Jan 10 '08 #6

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

Similar topics

3
2596
by: Ian | last post by:
Hi all, I have a problem. I have an application which needs to work with a lot of data, but not all at the same time. It is arranged as a set of objects, each with lots of data that is created when the object is instantiated. I'd ideally like to keep as many objects as possible in memory, but I can get rid of any object the program isn't currently using. Is there any way I can access the amount of memory python is using? I can
8
3677
by: rbt | last post by:
Would a Python process consume more memory on a PC with lots of memory? For example, say I have the same Python script running on two WinXP computers that both have Python 2.4.0. One computer has 256 MB of Ram while the other has 2 GB of Ram. On the machine with less Ram, the process takes about 1 MB of Ram. On the machine with more Ram, it uses 9 MB of Ram. Is this normal and expected behavior?
3
4152
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database periodically. What happens, is that the app reads the contents of a text file line by line into an ArrayList. Each element of the ArrayList is a string representing a record from the file. The ArrayList is then processed, and the arraylist goes out of...
4
3432
by: Hermann Maier | last post by:
hi, i need to find out the memory usage of a specific function that i use in my program. this function does some recursive calculations and i want my program to display the amount of memory the function used to calculate a specific value. thx
7
2560
by: Fernando Barsoba | last post by:
Hi, After following the advice received in this list, I have isolated the memory leak problem I am having. I am also using MEMWATCH and I think it is working properly. The program does some calculations and stores elements in a list. After that, a sorting algorithm is used over that list. Two functions are called before the sorting process:
20
9373
by: mariano.difelice | last post by:
Hi, I've a big memory problem with my application. First, an example: If I write: a = range(500*1024) I see that python process allocate approximately 80Mb of memory.
13
4494
by: placid | last post by:
Hi All, Just wondering when i run the following code; for i in range(1000000): print i the memory usage of Python spikes and when the range(..) block finishes execution the memory usage does not drop down. Is there a way of freeing this memory that range(..) allocated?
17
8489
by: frederic.pica | last post by:
Greets, I've some troubles getting my memory freed by python, how can I force it to release the memory ? I've tried del and gc.collect() with no success. Here is a code sample, parsing an XML file under linux python 2.4 (same problem with windows 2.5, tried with the first example) : #Python interpreter memory usage : 1.1 Mb private, 1.4 Mb shared #Using http://www.pixelbeat.org/scripts/ps_mem.py to get memory information
3
2472
by: crazy420fingers | last post by:
I'm running a python program that simulates a wireless network protocol for a certain number of "frames" (measure of time). I've observed the following: 1. The memory consumption of the program grows as the number of frames I simulate increases. To verify this, I've used two methods, which I invoke after every frame simulated:
0
10455
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10228
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10173
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6788
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.