Table of Contents
How Python manages memory" >How Python manages memory
Reference Cycles in Python" >Reference Cycles in Python
Python Garbage Collector (gc)" >Python Garbage Collector (gc)
如何使用 gc 模块" >如何使用 gc 模块
使用 gc 调试垃圾收集" >使用 gc 调试垃圾收集
避免 Python 内存管理中的陷阱" >避免 Python 内存管理中的陷阱
注意对象范围" >注意对象范围
使用 weakref避免引用循环" >使用 weakref避免引用循环
Manual Breaking of Reference Cycles" >Manual Breaking of Reference Cycles
Home Backend Development Python Tutorial Do you understand how Python memory management works?

Do you understand how Python memory management works?

Apr 12, 2023 pm 04:25 PM
python Memory management

Python offers many conveniences to developers, one of the biggest being its virtually worry-free memory management. Developers no longer need to manually allocate, track, and free memory for objects and data structures in Python. The runtime does all this work for you, so you can focus on solving actual problems rather than wrangling machine-level details.

Do you understand how Python memory management works?

# Still, even for inexperienced Python users, it’s beneficial to understand how Python’s garbage collection and memory management work. Understanding these mechanisms will help you avoid performance issues that may arise with more complex projects. You can also use Python's built-in tools to monitor your program's memory management behavior.

How Python manages memory

Every Python object has a reference count, also called a reference count. refcount is a count of the total number of other objects that hold references to a given object. As you add or remove references to the object, the number goes up or down. When an object's reference count reaches zero, the object is deallocated and its memory is freed.

What is a reference? Allows access to any content of an object by name or through an accessor in another object.

Here's a simple example:

x = "Hello there"
Copy after login

When we issue this command to Python, two things happen under the hood:

  1. The string "Hello there" is created as a Python object and stored in memory.
  2. The name x is created in the local namespace and points to the object, which increases its reference count by 1 to 1.

If we say y = x, then the reference count will increase to 2 again.

Whenever xandy goes out of scope or is removed from their namespace, the string's reference count is reduced by 1 for each name. Once both x and y go out of range or are deleted, the string's reference count becomes 0 and is deleted.

Now, suppose we create a list containing a string as follows:

x = ["Hello there", 2, False]
Copy after login

The string remains in memory until the list itself is deleted or the element containing the string is removed from the list Delete in. Either of these operations will cause the only thing holding a reference to the string to disappear.

Now consider this example:

x = "Hello there" y = [x]
Copy after login

If we remove the first element y from , or delete the list y entirely, the string is still in memory. This is because the name x contains a reference to it.

Reference Cycles in Python

In most cases, reference counting works fine. But sometimes you encounter a situation where two objects each hold a reference to the other. This is called the reference period. In this case, the object's reference count never reaches zero and it is never deleted from memory.

This is a contrived example:

x = SomeClass()
y = SomeOtherClass()
x.item = y
y.item = x
Copy after login

Since x and y hold references to each other, they are never deleted from the system - even if nothing else references either of them anyone.

It's actually quite common for Python's own runtime to generate reference cycles for objects. An example is an exception with a traceback object that contains a reference to the exception itself.

In earlier versions of Python, this was a problem. Objects with reference cycles can accumulate over time, which is a big problem for long-running applications. But Python has since introduced cycle detection and garbage collection systems to manage reference cycles.

Python Garbage Collector (gc)

Python’s garbage collector detects objects with reference cycles. It does this by keeping track of objects that are "containers" (e.g. lists, dictionaries, custom class instances) and determining which of them are not accessible anywhere else.

Once these objects are picked out, the garbage collector deletes them by ensuring that their reference counts can safely drop to zero.

The vast majority of Python objects have no reference cycles, so the garbage collector does not need to run 24/7. Instead, the garbage collector uses some heuristics to run less frequently and run as efficiently as possible every time.

When the Python interpreter starts, it keeps track of the number of objects that have been allocated but not freed. The vast majority of Python objects are short-lived, so they appear and disappear quickly. But over time, more long-lived objects will emerge. Once more than a certain number of such objects accumulate, the garbage collector runs.

Every time the garbage collector runs, it collects all objects that survived the collection and places them in a group called a generation. These "first generation" objects are scanned less frequently during the reference cycle. Any first-generation objects that survive the garbage collector will eventually be migrated to second-generation, where they are scanned less frequently.

同样,垃圾收集器不会跟踪所有内容。例如,像用户创建的类这样的复杂对象总是被跟踪。但是不会跟踪仅包含简单对象(如整数和字符串)的字典,因为该特定字典中的任何对象都不会包含对其他对象的引用。不能保存对其他元素(如整数和字符串)的引用的简单对象永远不会被跟踪。

如何使用 gc 模块

通常,垃圾收集器不需要调整即可运行良好。Python 的开发团队选择了反映最常见现实世界场景的默认值。但是如果你确实需要调整垃圾收集的工作方式,你可以使用Python 的 gc 模块。该gc模块为垃圾收集器的行为提供编程接口,并提供对正在跟踪的对象的可见性。

gc当你确定不需要垃圾收集器时,你可以做的一件有用的事情是关闭它。例如,如果你有一个堆放大量对象的短运行脚本,则不需要垃圾收集器。脚本结束时,所有内容都将被清除。为此,你可以使用命令禁用垃圾收集器gc.disable()。稍后,你可以使用 重新启用它gc.enable()。

你还可以使用 手动运行收集周期gc.collect()。一个常见的应用是管理程序的性能密集型部分,该部分会生成许多临时对象。你可以在程序的该部分禁用垃圾收集,然后在最后手动运行收集并重新启用收集。

另一个有用的垃圾收集优化是gc.freeze(). 发出此命令时,垃圾收集器当前跟踪的所有内容都被“冻结”,或者被列为免于将来的收集扫描。这样,未来的扫描可以跳过这些对象。如果你有一个程序在启动之前导入库并设置大量内部状态,那么你可以gc.freeze()在所有工作完成后发出。这使垃圾收集器不必搜寻那些无论如何都不太可能被删除的东西。(如果你想对冻结的对象再次执行垃圾收集,请使用gc.unfreeze().)

使用 gc 调试垃圾收集

你还可以使用它gc来调试垃圾收集行为。如果你有过多的对象堆积在内存中并且没有被垃圾收集,你可以使用gc's 检查工具来找出可能持有对这些对象的引用的对象。

如果你想知道哪些对象持有对给定对象的引用,可以使用gc.get_referrers(obj)列出它们。你还可以使用gc.get_referents(obj)来查找给定对象引用的任何对象。

如果你不确定给定对象是否是垃圾收集的候选对象,gc.is_tracked(obj)请告诉你垃圾收集器是否跟踪该对象。如前所述,请记住垃圾收集器不会跟踪“原子”对象(例如整数)或仅包含原子对象的元素。

如果你想亲自查看正在收集哪些对象,可以使用 设置垃圾收集器的调试标志gc.set_debug(gc.DEBUG_LEAK|gc.DEBUG_STATS)。这会将有关垃圾收集的信息写入stderr。它将所有作为垃圾收集的对象保留在只读列表中。

避免 Python 内存管理中的陷阱

如前所述,如果你在某处仍有对它们的引用,则对象可能会堆积在内存中而不会被收集。这并不是 Python 垃圾收集本身的失败。垃圾收集器无法判断你是否不小心保留了对某物的引用。

让我们以一些防止对象永远不会被收集的指针作为结尾。

注意对象范围

如果你将对象 1 指定为对象 2 的属性(例如类),则对象 2 将需要超出范围,然后对象 1 才会:

obj1 = MyClass()
obj2.prop = obj1
Copy after login

更重要的是,如果这种情况发生在某种其他操作的副作用中,例如将对象 2 作为参数传递给对象 1 的构造函数,你可能不会意识到对象 1 持有一个引用:

obj1 = MyClass(obj2)
Copy after login

另一个例子:如果你将一个对象推入模块级列表并忘记该列表,则该对象将一直保留,直到从列表中删除,或者直到列表本身不再有任何引用。但是如果该列表是一个模块级对象,它可能会一直存在,直到程序终止。

简而言之,请注意你的对象可能被另一个看起来并不总是很明显的对象持有的方式。

使用 weakref避免引用循环

Python 的 weakref 模块允许你创建对其他对象的弱引用。弱引用不会增加对象的引用计数,因此只有弱引用的对象是垃圾回收的候选对象。

一个常见的用途weakref是对象缓存。你不希望仅仅因为它具有缓存条目而保留引用的对象,因此你将 aweakref用于缓存条目。

Manual Breaking of Reference Cycles

Finally, if you know that a given object contains a reference to another object, you can always manually break a reference to that object. For example, if you have instance_of_class.ref = other_object, you can set instance_of_class.ref = None when ready to delete instance_of_class.

By understanding how Python memory management works, we take a look at how its garbage collection system helps optimize memory in Python programs, and how you can control memory usage and garbage collection using modules provided by the standard library and elsewhere.

Original title:​​Python garbage collection and the gc module​

The above is the detailed content of Do you understand how Python memory management works?. For more information, please follow other related articles on the PHP Chinese website!

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the conversion speed fast when converting XML to PDF on mobile phone? Is the conversion speed fast when converting XML to PDF on mobile phone? Apr 02, 2025 pm 10:09 PM

The speed of mobile XML to PDF depends on the following factors: the complexity of XML structure. Mobile hardware configuration conversion method (library, algorithm) code quality optimization methods (select efficient libraries, optimize algorithms, cache data, and utilize multi-threading). Overall, there is no absolute answer and it needs to be optimized according to the specific situation.

Is there any mobile app that can convert XML into PDF? Is there any mobile app that can convert XML into PDF? Apr 02, 2025 pm 08:54 PM

An application that converts XML directly to PDF cannot be found because they are two fundamentally different formats. XML is used to store data, while PDF is used to display documents. To complete the transformation, you can use programming languages ​​and libraries such as Python and ReportLab to parse XML data and generate PDF documents.

How to convert XML files to PDF on your phone? How to convert XML files to PDF on your phone? Apr 02, 2025 pm 10:12 PM

It is impossible to complete XML to PDF conversion directly on your phone with a single application. It is necessary to use cloud services, which can be achieved through two steps: 1. Convert XML to PDF in the cloud, 2. Access or download the converted PDF file on the mobile phone.

What is the function of C language sum? What is the function of C language sum? Apr 03, 2025 pm 02:21 PM

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

How to control the size of XML converted to images? How to control the size of XML converted to images? Apr 02, 2025 pm 07:24 PM

To generate images through XML, you need to use graph libraries (such as Pillow and JFreeChart) as bridges to generate images based on metadata (size, color) in XML. The key to controlling the size of the image is to adjust the values ​​of the <width> and <height> tags in XML. However, in practical applications, the complexity of XML structure, the fineness of graph drawing, the speed of image generation and memory consumption, and the selection of image formats all have an impact on the generated image size. Therefore, it is necessary to have a deep understanding of XML structure, proficient in the graphics library, and consider factors such as optimization algorithms and image format selection.

How to open xml format How to open xml format Apr 02, 2025 pm 09:00 PM

Use most text editors to open XML files; if you need a more intuitive tree display, you can use an XML editor, such as Oxygen XML Editor or XMLSpy; if you process XML data in a program, you need to use a programming language (such as Python) and XML libraries (such as xml.etree.ElementTree) to parse.

How to convert xml into pictures How to convert xml into pictures Apr 03, 2025 am 07:39 AM

XML can be converted to images by using an XSLT converter or image library. XSLT Converter: Use an XSLT processor and stylesheet to convert XML to images. Image Library: Use libraries such as PIL or ImageMagick to create images from XML data, such as drawing shapes and text.

Recommended XML formatting tool Recommended XML formatting tool Apr 02, 2025 pm 09:03 PM

XML formatting tools can type code according to rules to improve readability and understanding. When selecting a tool, pay attention to customization capabilities, handling of special circumstances, performance and ease of use. Commonly used tool types include online tools, IDE plug-ins, and command-line tools.

See all articles