Talking about memory usage of Python objects, talking about memory usage of python objects
Everything is an object
In Python, everything is an object, including all types of constants and variables, integer, Boolean, and even functions. See a problem in stackoverflow: everything an object in python like ruby.
The code can be verified:
# Everythin in python is object def fuction (): return print isinstance (True, object) print isinstance (0, object) print isinstance ('A', object) print isinstance (fuction, object)
How to calculate
Python provides the getsizeof function in the sys module to calculate the Python object size.
Sys. getsizeof (object [, default]) returns the object size in bytes. This object can be any type of object. Therefore, all built-in objects can return correct results, but they are not guaranteed to be valid for third-party extensions because they are related to specific implementations ....... Getsizeof () calls the _ sizeof _ method of the object. If the object is managed by the garbage collector, additional Garbage Collector overhead will be added.
Of course, the object memory usage is closely related to the Python version and operating system version. The code and test results in this article are based on the Windows 7 32-bit operating system.
Import sys print sys. version
2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)]
Basic Type
• Boolean
Print 'size of True: % d' % (sys. getsizeof (True) print 'size of False: % d' % (sys. getsizeof (False ))
Output:
Size of True: 12 size of False: 12
• Integer
# Normal integer print 'size of integer: % d' % (sys. getsizeof (1) # long print 'size of long integer: % d' % (sys. getsizeof (1L) print 'size of big long integer: % d' % (sys. getsizeof (100000L) output:
Size of integer: 12x size of long integer 1L: 14 size of long integer 100000L: 16
It can be seen that the integer occupies 12 bytes, the long integer occupies at least 14 bytes, And the occupied space will increase with the increase of the number of digits. In 2.x, if the integer value exceeds sys. maxint, it is automatically extended to a long integer. After Python 3.0, the integer and long integer types are unified into one type.
• Floating point type
Print 'size of float: % d' % (sys. getsizeof (1.0 ))
Output:
Size of float: 16
The floating point occupies 16 bytes. After a certain precision is exceeded, it is rounded up.
Refer to the following code:
Print 1.00000000003 print 1.000000000005
Output:
1.00000000003 1.00000000001
• String
# Size of string type print '\ r \ n '. join (["size of string with % d chars: % d" % (len (elem), sys. getsizeof (elem) for elem in ["", "a", "AB"]) # size of unicode string print '\ r \ n '. join (["size of unicode string with % d chars: % d" % (len (elem), sys. getsizeof (elem) for elem in [u "", u "a", u "AB"])
Output:
Size of string with 0 chars: 21 size of string with 1 chars: 22 size of string with 2 chars: 23 size of unicode string with 0 chars: 26 size of unicode string with 1 chars: 28 size of unicode string with 2 chars: 30
A normal null string occupies 21 bytes. Each added character occupies 1 byte. A Unicode string occupies at least 26 bytes. Each added character occupies 2 more bytes.
Set Type
• List
# Size of list type print '\ r \ n '. join (["size of list with % d elements: % d" % (len (elem), sys. getsizeof (elem) for elem in [[], [0], [], [, 2])
Output:
Size of list with 0 elements: 36 size of list with 1 elements: 40 size of list with 2 elements: 44 size of list with 3 elements: 48
It can be seen that the List occupies at least 36 bytes. Each added element adds 4 bytes. However, the sys. getsizeof function does not calculate the element size of the container type. For example:
Print 'size of list with 3 integers % d' % (sys. getsizeof ([0, 1, 2]) print 'size of list with 3 strings % d' % (sys. getsizeof (['0', '1', '2'])
Output:
Size of list with 3 integers 48 size of list with 3 strings 48
The reference to elements should be saved in the container. To accurately calculate containers, refer to recursive sizeof recipe. Use the total_size function:
Print 'total size of list with 3 integers % d' % (total_size ([0, 1, 2]) print 'total size of list with 3 strings % d' % (total_size (['0', '1', '2'])
Output:
Total size of list with 3 integers 84 total size of list with 3 strings 114
It can be seen that the space occupied by the list is 36 + basic space (Object Reference 4 + object size) * Number of elements.
In addition, if you declare a list variable, it will allocate some space in advance to Increase the efficiency when adding elements:
Li = [] for I in range (0,101): print 'list with % d integers size: % d, total_size: % d' % (I, getsizeof (li ), total_size (li) li. append (I)
• Tuples
Similar to the list, but it occupies at least 28 bytes.
• Dictionary
The dictionary is much more complex. For details, see the code dictobject. c. In addition, notes on optimizing dictionaries is worth reading carefully.
For the basic information, see some answers in [stackoverflow] to Python's underlying hash data structure for dictionaries:
• The dictionary has a minimum space of 8 entries (PyDict_MINSIZE );
• When the number of entries is less than 50,000, the number of entries increases by 4 times each time;
• When the number of entries exceeds 50,000, the number of entries increases by 2 times each time;
• The hash value of the key is cached in the dictionary. The dictionary size is not recalculated;
The dictionary is adjusted every time it approaches 2/3.
The above discussion about the memory usage of Python objects is all the content shared by Alibaba Cloud. I hope to give you a reference and support for the house of helpers.