Is there any way to minimize str()/unicode() objects memory usage [Python 2.6.4] ?

Christian Heimes lists at cheimes.de
Sat Aug 7 04:07:13 CEST 2010


> I'm running into some performance / memory bottlenecks on large lists.
> Is there any easy way to minimize/optimize memory usage?
> 
> Simple str() and unicode objects() [Python 2.6.4/Linux/x86]:
>>>> sys.getsizeof('')      24 bytes
>>>> sys.getsizeof('0')    25 bytes
>>>> sys.getsizeof(u'')    28 bytes
>>>> sys.getsizeof(u'0')  32 bytes

A Python str object contains much more than just the raw string. On a
32bit system it contains:

 * a pointer to its type (ptr with 4 bytes)
 * a reference counter (ssize_t, 4 bytes)
 * the length of the string (ssize_t, 4 bytes)
 * the cached hash of the string (long, 8 bytes)
 * interning state (int, 4 bytes)
 * a null terminated char array for its data.

Christian





More information about the Python-list mailing list