Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory management in Python

Tags:

python

I'm new to python.To find the sizeof an integer i used getsizeof method available in sys module. It returns 24 bytes for integer and 34 bytes for char.

>>> sys.getsizeof(1)
24
>>> sys.getsizeof('a')
34

I feel this size (24 bytes or 34 bytes) is very large to hold an integer or char... I feel that memory is getting wasted much.. Could you please help me in understanding the concept behind this memory management in python.

like image 628
rpraj Avatar asked Jul 10 '11 08:07

rpraj


1 Answers

Because everything is an object, everything has an object bookkeeping overhead. In CPython, it's at least size of a type pointer and reference count for every object. Plus whatever specific objects need for their data. And there's also garbage collector overhead for some objects. Certainly nothing is 'wasted', it's a silly idea.

And there is no char in Python, 'a' is a string of length 1.

like image 171
Cat Plus Plus Avatar answered Oct 21 '22 13:10

Cat Plus Plus