Python 是否有堆栈/堆以及如何管理内存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14546178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:47:51  来源:igfitidea点击:

Does Python have a stack/heap and how is memory managed?

pythonmemorymemory-management

提问by Matt Alcock

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

采纳答案by NlightNFotis

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.

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

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

它有堆栈和堆吗?用什么算法来管理内存?

When we are talking about CPythonit uses a private heapfor storing objects. From the CPython C API documentation:

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

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 中的内存管理涉及一个包含所有 Python 对象和数据结构的私有堆。这个私有堆的管理是由 Python 内存管理器在内部确保的。Python 内存管理器具有处理各种动态存储管理方面的不同组件,例如共享、分段、预分配或缓存。

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 algorithmsthat try to find all the reachable objects.

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

NOTE:Please keep in mind that this information is CPythonspecific. Other python implementations, such as pypy, iron python, jythonand 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

注意:请记住,此信息是CPython特定的。其他Python实现,比如pypyiron pythonjython当谈到自己的实现细节和其他人可能来自彼此之间以及与CPython的不同。为了更好地理解这一点,理解 Python 的语义(语言)和底层实现之间存在差异可能会有所帮助

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

鉴于这些知识,是否有关于大量/数据处理的内存管理的建议?

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.

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

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

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

回答by phant0m

Python doesn't have anysuch thing.

Python没有任何这样的东西。

Python is the languageand does not specify how exactly implementationsmust achieve the semantics defined by Python the language.

Python 是语言,并没有指定实现必须如何准确地实现Python 语言定义的语义。

Every implementation (CPython, PyPy, IronPython, Stackless, Jython...) is free to do its own thing!

每个实现(CPython、PyPy、IronPython、Stackless、Jython...)都可以自由地做自己的事情!

In CPython, allobjects live on the heap:

CPython 中,所有对象都存在于堆中:

Memory management in Python involves a private heap containing all Python objects and data structures.1

Python 中的内存管理涉及一个包含所有 Python 对象和数据结构的私有堆。1

The CPython virtual machine is stack based:

CPython 虚拟机是基于堆栈的:

>>> def g():
    x = 1
    y = 2
    return f(x, y)

>>> import dis
>>> dis.dis(g)
  2           0 LOAD_CONST           1 (1) # Push 1 onto the stack
              3 STORE_FAST           0 (x) # Stores top of stack into local var x

  3           6 LOAD_CONST           2 (2) # Push 2 onto stack
              9 STORE_FAST           1 (y) # Store TOS into local var y

  4          12 LOAD_GLOBAL          0 (f) # Push f onto stack
             15 LOAD_FAST            0 (x) # Push x onto stack
             18 LOAD_FAST            1 (y) # Push y onto stack
             21 CALL_FUNCTION        2     # Execute function with 2 
                                           # f's return value is pushed on stack
             24 RETURN_VALUE               # Return TOS to caller (result of f)

Keep in mind, that this is CPython specific. The stack does not contain the actualvalues though, it keeps references to those objects.

请记住,这是特定于 CPython 的。虽然堆栈不包含实际值,但它保留对这些对象的引用。

1: Source

1来源