search
HomeBackend DevelopmentPython TutorialPython object memory usage

Python object memory usage

Feb 28, 2017 pm 04:48 PM

Everything is an object

Everything in Python is an object, including all types of constants and variables, integers, Boolean types, 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 Python objects the size of.

sys.getsizeof(object[, default])

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

......

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

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

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, and the long integer The type occupies at least 14 bytes, and the space occupied will become larger as the number of bits 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 type

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 '\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

Normal null character The 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 '\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 list with 1 elements: 40 size of list with 2 elements: 44 size of list with 3 elements: 48

The visible list occupies the least 36 bytes, each additional element adds 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 given by it:

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 grow 4 times each time;
•When the number of entries is greater than 50,000, it will grow 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 be resized every time it approaches 2/3.

The above article on Python object memory usage is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.

For more articles related to Python object memory usage, please pay attention to the PHP Chinese website!

Statement
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
Python vs. C  : Understanding the Key DifferencesPython vs. C : Understanding the Key DifferencesApr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Python vs. C  : Which Language to Choose for Your Project?Python vs. C : Which Language to Choose for Your Project?Apr 21, 2025 am 12:17 AM

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)