On the memory consumption of Python objects

Source: Internet
Author: User
everything is an object .

In Python everything is an object, including constants and variables of all types, integers, booleans, and even functions. See a question on StackOverflow is everything a object in python like Ruby

The code can verify that:

# Everythin in Python is Object Def fuction (): Return print isinstance (True, object) print isinstance (0, object) print ISI Nstance (' A ', object) print Isinstance (Fuction, Object)

How to calculate

Python provides the function getsizeof in the SYS module to calculate the size of the Python object.

Sys.getsizeof (object[, default]) returns the size of the object in bytes (byte). This object can be any type of object. Therefore, the built-in objects can return the correct results but are not guaranteed to be valid for third-party extensions because they are related to specific implementations. ... getsizeof () invokes the __sizeof__ method of the object, which, if the object is managed by the garbage collector, adds additional garbage collector overhead.

Of course, object memory consumption is closely related to the Python version and the operating system version, and the Code and test results for this article are based on the Windows7 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 type

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

• Integral type

# 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

As you can see, the integer type occupies 12 bytes, and the long integer occupies a minimum of 14 bytes, and the footprint becomes larger as the number of bits increases. In the 2.x version, if the value of an integral type exceeds sys.maxint, it is automatically extended to a long integer type. After Python 3.0, integer and long integers are unified into one type.

• Floating-point type

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

Output:

Size of Float:16

Floating-point type occupies 16 bytes. After a certain amount of precision will be rounded.

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 the 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 a string with 2 chars:23 size of Unicode string wit H 0 chars:26 size of Unicode string with 1 chars:28 size of the Unicode string with 2 chars:30

The normal empty string takes up 21 bytes, each of which is incremented by 1 bytes. Unicode strings occupy a minimum of 26 bytes, each with one additional character and 2 more bytes.

Collection 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], [0,2], [0,1,2]]

Output:

Size of list with 0 elements:36 size of the list with 1 elements:40 size of the list with 2 elements:44 size of the list with 3 ele ments:48

The visible list occupies a minimum of 36 bytes, with each additional element increasing by 4 bytes. Note, however, that the Sys.getsizeof function does not calculate the element size of the container type. Like what:

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-size of list with 3 strings 48

The container should hold a reference to the element. If you want to calculate the container accurately, you can refer to recursive sizeof recipe. Use the Total_size function that it gives:

print ' total size of the list with 3 integers%d '% (Total_size ([0,1,2])) print ' Total size of the list with 3 strings%d '% (tota L_size ([' 0 ', ' 1 ', ' 2 ')])

The output is:

Total size of the list with 3 integers-total size of the list with 3 strings 114

You can see that the space of the list occupies a base space of 36 + (object reference 4 + object size) * Number of elements.

It is also important to 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)

• Meta-group

It is basically similar to a list, but it occupies a minimum of 28 bytes.

• Dictionaries

The situation of the dictionary is relatively complex, of course, refer to the code dictobject.c, in addition to NOTES on optimizing dictionaries is worth reading carefully.

The basic situation can be referred to in [StackOverflow] question Python's underlying hash data structure for dictionaries some of the answers:

• The dictionary has a minimum space of 8 entries (pydict_minsize);
• The number of entries is less than 50,000 and increases by 4 times times each time;
• When the number of entries is greater than 50,000, each increase is twice times;
• The hash value of the key is cached in the dictionary and is not recalculated after the dictionary is resized;

The dictionary resizes every close to 2/3.

This article on the Python object memory footprint is a small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we support the script home.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.