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. 5 25692
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>
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.greenend.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
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
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 ... :-?
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):
"""APPROXIMATE 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_collector = 8
else:
garbage_collector = 0
malloc = 8 # in most cases
size = size + overhead + garbage_collector + 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
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...
|
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...
|
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 projectplanning, coding, testing,...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
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 ...
| |