CPython - 在内部,堆栈和堆上存储什么?

发布于 2024-08-22 22:25:00 字数 132 浏览 6 评论 0原文

在 C# 中,值类型(例如:int、float 等)存储在堆栈中。方法参数也可以存储在堆栈中。然而,大多数其他内容都存储在堆上。这包括列表、对象等。

我想知道,CPython 内部是否也做同样的事情?它在栈上存储什么,在堆上又放什么?

In C#, Value Types (eg: int, float, etc) are stored on the stack. Method parameters may also be stored on the stack as well. Most everything else, however, is stored on the heap. This includes Lists, objects, etc.

I was wondering, does CPython do the same thing internally? What does it store on the stack, and what does it put on the heap?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说好的呢 2024-08-29 22:25:00

CPython 实现中的所有 Python 对象都位于堆上。您可以在此处 在文档中:

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

请注意,Python 本身只是一种语言,并没有说明内存管理等内部机制应该如何工作;这是留给实施者的细节。

All Python objects in the CPython implementation go on the heap. You can read in detail how Python's memory management works here in the documentation:

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.

Note that Python itself is just a language, and says nothing about how internals like memory management should work; this is a detail left to implementers.

梦里寻她 2024-08-29 22:25:00

Python 的运行时仅处理对对象的引用(它们都位于堆中):Python 堆栈上的内容(作为其字节码操作的操作数和结果)始终是引用(对位于其他位置的值)。

Python's runtime only deals in references to objects (which all live in the heap): what goes on Python's stack (as operands and results of its bytecode operations) are always references (to values that live elsewhere).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文