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

查看:147
本文介绍了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和其他实现可能在细节上与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:

  • Stepping through CPython (video)
  • A presentation about the internals of the Python Virtual Machine
  • In true hacker spirit, the CPython Object Allocator source code

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

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