Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 420
Matt Alcock Avatar asked Jan 27 '13 09:01

Matt Alcock


People also ask

Does Python use stack or heap memory?

According to the Python documentation (3.9. 0) for memory management, Python's memory management involves a private heap that is used to store your program's objects and data structures.

How does Python use stack and heap?

Memory Allocation in PythonThe methods/method calls and the references are stored in stack memory and all the values objects are stored in a private heap.

How is heap managed in Python?

As we know, Python uses the dynamic memory allocation which is managed by the Heap data structure. Memory Heap holds the objects and other data structures that will be used in the program. Python memory manager manages the allocation or de-allocation of the heap memory space through the API functions.

How does Python manage memory?

Memory allocation can be defined as allocating a block of space in the computer memory to a program. In Python memory allocation and deallocation method is automatic as the Python developers created a garbage collector for Python so that the user does not have to do manual garbage collection.


1 Answers

How are variables and memory managed in 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?

When we are talking about CPython it uses a private heap for storing objects. From the CPython C API 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.

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.

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?

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.

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
like image 143
NlightNFotis Avatar answered Sep 22 '22 19:09

NlightNFotis