Tree structure consuming lot of memory
sajmikins at gmail.com
Mon Jul 6 13:15:22 EDT 2009
On Mon, Jul 6, 2009 at 6:12 AM, mayank gupta<mooniitk at gmail.com> wrote:
> Thanks for the other possibilites. I would consider option (2) and (3) to
> improve my code.
> But out of curiosity, I would still like to know why does an object of a
> Python-class consume "so" much of memory (1.4 kb), and this memory usage has
> nothing to do with its attributes.
> On Mon, Jul 6, 2009 at 12:03 PM, Chris Rebert <clp2 at rebertia.com> wrote:
>> On Mon, Jul 6, 2009 at 2:55 AM, mayank gupta<mooniitk at gmail.com> wrote:
>> > Hi,
>> > I am creating a tree data-structure in python; with nodes of the tree
>> > created by a simple class :
>> > class Node :
>> > def __init__(self , .... other attributes):
>> > # initialise the attributes here!!
>> > But the problem is I am working with a huge tree (millions of nodes);
>> > and
>> > each node is consuming much more memory than it should. After a little
>> > analysis, I found out that in general it uses about 1.4 kb of memory for
>> > each node!!
>> > I will be grateful if someone could help me optimize the memory usage.
>> (1) Use __slots__ (see
>> (2) Use some data structure other than a tree
>> (3) Rewrite your Node/Tree implementation in C
For option 2 you should try using the built in types, list tuple or
dict. You might get better results.
I'm curious too as to why the class/instance code should take so much
memory, could you mention more about the code?
More information about the Python-list