memory usage

Troels Therkelsen t_therkelsen at hotmail.com
Tue May 6 12:33:32 EDT 2003


In article <mailman.1052227330.15773.python-list at python.org>, Nagy Gabor wrote:
[snip]

> class T:
>     def __init__(self, Value = '', Flag = 0, Name = '', Class = ''):
>         self.Flag = Flag
> 	self.Value = Value
> 	self.Class = Class
> 	self.Name = Name
> 
> class TD:
>     def __init__(self):
>         self.Tag = T()
> 	self.Data = None
> 

Since you write elsewhere in your post that these classes gets instantiated
nearly 600000 times (I couldn't figure out if they stay in memory for the
duration of the program but it would appear so), maybe you want to look into
using __slots__ as this will help a lot on memory usage if you're having
2*600000 classes in memory. Eg.,

class TD:
    __slots__ = ['Tag', 'Data']
    def __init__(self):
        self.Tag = T()
        self.Data = None

Alternatively, if you don't plan on further complicating T and TD, you might
want to consider using a datatype with less overhead, like a list or a tuple.

Regards,

Troels Therkelsen





More information about the Python-list mailing list