Python 是否有堆栈/堆以及如何管理内存? [英] Does Python have a stack/heap and how is memory managed?

查看:24
本文介绍了Python 是否有堆栈/堆以及如何管理内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 中如何管理变量和内存?它有堆栈和堆吗?用什么算法来管理内存?鉴于这些知识,是否有关于大量/数据处理的内存管理的任何建议?

How are variables and memory managed in Python? Does it have a stack and a heap and what algorithm is used to manage memory? Given this knowledge are there any recommendations on memory management for large number/data crunching?

推荐答案

Python 中如何管理变量和内存.

How are variables and memory managed in Python.

自动!不,实际上,您只需创建一个对象,Python 虚拟机就会处理所需的内存以及它应放置在内存布局中的位置.

Automagically! No, really, you just create an object and the Python Virtual Machine handles the memory needed and where it shall be placed in the memory layout.

有没有栈和堆,用什么算法来管理内存?

Does it have a stack and a heap and what algorithm is used to manage memory?

当我们谈论 CPython 时,它使用 私有堆 来存储对象.来自 CPython C API 文档:

When we are talking about CPython it uses a private heap for storing objects. From the CPython C API documentation:

Python 中的内存管理涉及一个私有堆,其中包含所有Python 对象和数据结构.这个私人的管理堆由 Python 内存管理器内部确保.蟒蛇内存管理器有不同的组件来处理各种动态存储管理方面,如共享、分段、预分配或缓存.

Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager. The Python memory manager has different components which deal with various dynamic storage management aspects, like sharing, segmentation, preallocation or caching.

内存回收主要由引用计数处理.也就是说,Python VM 会在内部记录有多少引用引用了一个对象,并在没有更多引用引用它时自动对其进行垃圾收集.此外,还有一种打破循环引用的机制(引用计数无法处理)通过检测无法访问的对象孤岛",与传统 GC 有点相反算法试图找到所有可到达的对象.

Memory reclamation is mostly handled by reference counting. That is, the Python VM keeps an internal journal of how many references refer to an object, and automatically garbage collects it when there are no more references referring to it. In addition, there is a mechanism to break circular references (which reference counting can't handle) by detecting unreachable "islands" of objects, somewhat in reverse of traditional GC algorithms that try to find all the reachable objects.

注意:请记住,此信息是 CPython 特定的.其他 Python 实现,例如 pypyiron pythonjython 和其他 Python 实现在实现细节方面可能彼此不同,也可能与 CPython 不同.为了更好地理解这一点,理解 Python 的语义(语言)和底层实现之间存在差异可能会有所帮助

NOTE: Please keep in mind that this information is CPython specific. Other python implementations, such as pypy, iron python, jython and others may differ from one another and from CPython when it comes to their implementation specifics. To understand that better, it may help to understand that there is a difference between Python the semantics (the language) and the underlying implementation

鉴于这些知识,对于大量/数据处理的内存管理有什么建议吗?

Given this knowledge are there any recommendations on memory management for large number/data crunching?

现在我不能谈论这个,但我确信 NumPy(最流行的 Python 库数字运算)具有处理内存消耗的机制.

Now I can not speak about this, but I am sure that NumPy (the most popular python library for number crunching) has mechanisms that handle memory consumption gracefully.

如果您想了解更多关于 Python 内部的信息,请查看以下资源:

If you would like to know more about Python's Internals take a look at these resources:

这篇关于Python 是否有堆栈/堆以及如何管理内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆