A brief talk on Python object memory footprint _python

Source: Internet
Author: User
Tags in python

Everything is an object.

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

In the code, you can verify that:

# Everythin in Python are 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 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, built-in objects can return the correct results but are not guaranteed to be valid for Third-party extensions because they are related to a specific implementation.

......

Getsizeof () invokes the __sizeof__ method of the object, which, if it is managed by the garbage collector, adds additional garbage collector overhead.

Of course, the object memory footprint 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 to false:%d ' (sys.getsizeof (False))

Output:

Size of true:12 size of False:12

• Integral type

# Normal integer print ' size of:%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 100000l:16

You can see that the integral type occupies 12 bytes, the long integer occupies at least 14 bytes, and occupies space increases with the number of digits. In the 2.x version, if the value of an integral type exceeds sys.maxint, it is automatically extended to a long integer. After Python 3.0, integers 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

A floating-point type occupies 16 bytes. will be rounded after exceeding 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), 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 to 1 chars:22 size of string with 2 chars:23 size of Unicode string wit H 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 takes up 1 bytes for each additional character. Unicode strings occupy a minimum of 26 bytes, and each additional character takes up more than 2 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 to list with 1 elements:40 to size of list with 2 elements:44 size of list with 3 ele ments:48

The visible list occupies a minimum of 36 bytes, and increases by 4 bytes for each additional element. Note, however, that the sys.getsizeof function does not compute the element size of the container type. Like what:

print ' Size of list with 3 integers%d '% (sys.getsizeof ([0,1,2])) print ' Size to list with 3 strings%d ' (sys.getsizeof ([' 0 ', ' 1 ', ' 2 '])

Output:

The size of list with 3 integers of a list with 3 strings 48

A reference to an element should be stored in the container. If you want to accurately calculate the container, you can refer to recursive sizeof recipe. Use the Total_size function it gives:

print ' total size of a 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 a list with 3 integers total size of the list with 3 strings 114

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

Also note that if you declare a list variable, it allocates some space in advance to increase the efficiency when adding elements:

Li = [] for i in range (0): print ' list with%d integers size:%d, total_size:%d '% (i, getsizeof (LI), total_size (LI) ) Li.append (i)

• META Group

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

• Dictionaries

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

The basics can refer to [StackOverflow] for some answers in the Python ' s underlying hash data structure for dictionaries:

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

Every close to 2/3, the dictionary is resized.

This article on the Python object memory footprint is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.