Home > Backend Development > Python Tutorial > A brief discussion on Python object memory usage

A brief discussion on Python object memory usage

WBOY
Release: 2016-08-04 08:55:46
Original
1479 people have browsed it

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 stackoverflow Is everything an object in python like ruby

You can verify it in the code:

# 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.

sys.getsizeof(object[, default])

以字节(byte)为单位返回对象大小。 这个对象可以是任何类型的对象。 所以内置对象都能返回正确的结果 但不保证对第三方扩展有效,因为和具体实现相关。

......

getsizeof() 调用对象的 __sizeof__ 方法, 如果对象由垃圾收集器管理, 则会加上额外的垃圾收集器开销。
Copy after login

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 windows7 32-bit operating system.

import sys print sys.version

<font color="#000000" face="NSimsun">2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)]</font>

Basic types

•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

•Plastic surgery

# 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 type occupies 12 bytes, and the long integer type occupies at least 14 bytes, and the occupied space will become larger as the number of digits increases. In version 2.x, if the value of the integer type exceeds sys.maxint, it is automatically expanded to a long integer. After Python 3.0, integers and long integers are unified into one type.

•Floating point

print 'size of float: %d' % (sys.getsizeof(1.0))

Output:

size of float: 16

Floating point type occupies 16 bytes. Exceeding a certain precision will be rounded off.

Refer to the following code:

print 1.00000000003 print 1.000000000005

Output:

1.00000000003 1.00000000001

•String

# size of string type print 'rn'.join(["size of string with %d chars: %d" % (len(elem), sys.getsizeof(elem)) for elem in ["", "a" , "ab"]]) # size of unicode string print 'rn'.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

An ordinary empty string occupies 21 bytes, and each additional character occupies 1 more byte. Unicode strings occupy a minimum of 26 bytes, and each additional character occupies an additional 2 bytes.

Collection type

•List

# size of list type print 'rn'.join(["size of list with %d elements: %d" % (len(elem), sys.getsizeof(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 visible list occupies at least 36 bytes, and each additional element increases by 4 bytes. But be aware that 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

What is stored in the container should be a reference to the element. If you want to accurately calculate the container, you can refer to the recursive sizeof recipe. Use the total_size function it gives:

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

It can be seen that the space occupied by the list is basic space 36 + (object reference 4 + object size) * number of elements.

Also note that if you declare a list variable, it will pre-allocate 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)) li.append( i)

•Tuple

Basically similar to a list, but it occupies at least 28 bytes.

•Dictionary

The situation of dictionaries is relatively complicated. Of course, you should refer to the code dictobject.c for details. In addition, NOTES ON OPTIMIZING DICTIONARIES is worth reading carefully.

For the basic situation, you can refer to some answers in [stackoverflow] question 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, it will increase 4 times each time;
•When the number of entries is greater than 50,000, it will increase by 2 times each time;
•The hash value of the key is cached in the dictionary and will not be recalculated after the dictionary is resized;

The dictionary will resize every time it approaches 2/3.

The above article briefly discussing the memory usage of Python objects is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template