I worked out a small code which initializes about 1,000,000 nodes with some attributes, and saw the memory usage on my linux machine (using 'top' command). Then just later I averaged out the memory usage per node. I know this is not the most accurate way but just for estimated value.<br>
<br>The kind of Node class I am working on in my original code is like :<br><br>class Node:<br> def __init__(self, #attributes ):<br> self.coordinates = coordinates<br> self.index = index<br> self.sibNum = sibNum<br>
self.branchNum - branchNum<br><br>#here 'coordinates' and 'index' are LISTS with length = "dimension", where "dimension" is a user-input.<br><br>The most shocking part of it after the memory-analysis was that, the memory usage was never dependent on the "dimension". Yeah it varied a bit, but there wasnt any significant changes in the memory usage even when the "dimension" was doubled<br>
<br>-- Any clues?<br><br>Thank you for all your suggestions till this point.<br><br>Regards.<br><br><br><br><br><div class="gmail_quote">On Tue, Jul 7, 2009 at 1:28 AM, Antoine Pitrou <span dir="ltr"><<a href="mailto:solipsis@pitrou.net">solipsis@pitrou.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">mayank gupta <mooniitk <at> <a href="http://gmail.com" target="_blank">gmail.com</a>> writes:<br>
><br>
> After a little analysis, I found out that in general it uses about<br>
> 1.4 kb of memory for each node!!<br>
<br>
</div>How did you measure memory use? Python objects are not very compact, but 1.4KB<br>
per object seems a bit too much (I would expect more about 150-200 bytes/object<br>
in 32-bit mode, or 300-400 bytes/object in 64-bit mode).<br>
<br>
One of the solutions is to use __slots__ as already suggested. Another, which<br>
will have similar benefits, is to use a namedtuple. Both suppress the instance<br>
dictionnary (`instance`.__dict__), which is a major contributor to memory<br>
consumption. Illustration (64-bit mode, by the way):<br>
<br>
>>> import sys<br>
>>> from collections import namedtuple<br>
<br>
# First a normal class<br>
>>> class Node(object): pass<br>
...<br>
>>> o = Node()<br>
>>> o.value = 1<br>
>>> o.children = ()<br>
>>><br>
>>> sys.getsizeof(o)<br>
64<br>
>>> sys.getsizeof(o.__dict__)<br>
280<br>
# The object seems to take a mere 64 bytes, but the attribute dictionnary<br>
# adds a whoppy 280 bytes and bumps actual size to 344 bytes!<br>
<br>
# Now a namedtuple (a tuple subclass with property accessors for the various<br>
# tuple items)<br>
>>> Node = namedtuple("Node", "value children")<br>
>>><br>
>>> o = Node(value=1, children=())<br>
>>> sys.getsizeof(o)<br>
72<br>
>>> sys.getsizeof(o.__dict__)<br>
Traceback (most recent call last):<br>
File "<stdin>", line 1, in <module><br>
AttributeError: 'Node' object has no attribute '__dict__'<br>
<br>
# The object doesn't have a __dict__, so 72 bytes is its real total size.<br>
<font color="#888888"><br>
<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>I luv to walk in rain bcoz no one can see me crying<br>