Tree structure consuming lot of memory

Chris Rebert clp2 at rebertia.com
Mon Jul 6 06:03:00 EDT 2009


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 http://docs.python.org/reference/datamodel.html#slots)
(2) Use some data structure other than a tree
(3) Rewrite your Node/Tree implementation in C

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list