20

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?

0

2 Answers 2

22

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This does not seem to be entirely correct, at least for certain Python versions < 3.11, where some (possibly incidental) data is added to the interpreter process stack: stackoverflow.com/questions/3323001/…
14

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).

1 Comment

Would you explain more on what goes on Python's stack are always references or provide some reference?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.