Everything is an object.
Everything in Python is an object, including all types of constants and variables, integers, booleans, and even functions. See a question on * Is everything an object in python like ruby
The code can be verified right away:
# 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 function getsizeof in the sys module to calculate the size of Python objects.
(object[, default]) bytes(byte)Returns the size of the object in。 This object can be any type of object。 So the built-in objects all return the correct result However, it is not guaranteed to work for third-party extensions,Because of the relevance to specific implementations。 ...... getsizeof() Calling the object's __sizeof__ methodologies, If the object is managed by the garbage collector, then an additional garbage collector overhead is added!。
Of course, the memory footprint of an object is closely related to the version of Python and the version of the operating system, and the code and test results in this article are based on the windows 7 32-bit operating system.
import sys print
2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)]
basic type
-Boolean
print 'size of True: %d' % ((True)) print 'size of False: %d' % ((False))
Output:
size of True: 12 size of False: 12
-Integer
# normal integer print 'size of integer: %d' % ((1)) # long print 'size of long integer: %d' % ((1L)) print 'size of big long integer: %d' % ((100000L)) exports:
size of integer: 12x size of long integer 1L: 14 size of long integer 100000L: 16
It can be seen that integer types take up 12 bytes and long integer types take up a minimum of 14 bytes, and the space occupied becomes larger as the number of bits increases. In previous versions, if an integer type exceeded its value, it was automatically expanded to a long integer type. Since Python 3.0, integers and long integers have been unified into one type.
-floating point
print 'size of float: %d' % ((1.0))
Output:
size of float: 16
Floating point type occupies 16 bytes. It is rounded up beyond a certain precision.
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), (elem)) for elem in ["", "a", "ab"]]) # size of unicode string print '\r\n'.join(["size of unicode string with %d chars: %d" % (len(elem), (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
Normal null string occupies 21 bytes, each additional character occupies 1 more byte.Unicode string occupies at least 26 bytes, each additional character occupies 2 more bytes.
Collection type
-List
# size of list type print '\r\n'.join(["size of list with %d elements: %d" % (len(elem), (elem)) for elem in [[], [0], [0,2], [0,1,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
The minimum size of a list is 36 bytes, with an additional 4 bytes for each additional element. Note, however, that the function does not calculate the size of the elements of the container type. For example:
print 'size of list with 3 integers %d' % (([0,1,2])) print 'size of list with 3 strings %d' % ((['0','1','2']))
Output:
size of list with 3 integers 48 size of list with 3 strings 48
What is kept in the container should be a reference to the element. To calculate the container accurately, you can refer to therecursive sizeof recipe . Use its given 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']))
The output is:
total size of list with 3 integers 84 total size of list with 3 strings 114
You can see that the space occupied by the list is the base space 36 + (object reference 4 + object size) * number of elements.
Also note that if a list variable is declared, it is pre-allocated some space to increase 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)) (i)
-tuple
It is basically similar to a list, but its minimum occupancy is 28 bytes.
-Dictionary
The case of dictionaries is relatively much more complex, and the specifics are of course to bereference codeIn addition.NOTES ON OPTIMIZING DICTIONARIES Well worth a close read.
The basics can be found in [*]'s questionPython's underlying hash data structure for dictionaries Some of the answers in the
-Dictionary has a minimum space of 8 entries (PyDict_MINSIZE).
-4 times each time the number of entries is less than 50,000.
-The number of entries is increased by a factor of 2 each time the number of entries is greater than 50,000.
-key hash values are cached in a dictionary and are not recalculated after the dictionary is resized.
The dictionary resizes every time it approaches 2/3.
The above this talk about Python object memory occupation is all that I have shared with you, I hope to give you a reference, and I hope you will support me more.