Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>How are variables and memory managed in Python.</p> </blockquote> <p>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.</p> <blockquote> <p>Does it have a stack and a heap and what algorithm is used to manage memory?</p> </blockquote> <p>When we are talking about <code>CPython</code> it uses a <em>private heap</em> for storing objects. <a href="http://docs.python.org/2/c-api/memory.html" rel="noreferrer">From the CPython C API documentation</a>:</p> <blockquote> <p>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.</p> </blockquote> <p>Memory reclamation is mostly handled by <strong>reference counting</strong>. 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 href="https://github.com/python/cpython/blob/master/Modules/gcmodule.c" rel="noreferrer">a mechanism to break circular references</a> (which reference counting can't handle) by detecting unreachable "islands" of objects, <a href="http://www.arctrix.com/nas/python/gc/" rel="noreferrer">somewhat in reverse of traditional GC algorithms</a> that try to find all the reachable objects.</p> <p><strong><em>NOTE:</em></strong> Please keep in mind that this information is <strong><code>CPython</code></strong> specific. Other python implementations, such as <code>pypy</code>, <code>iron python</code>, <code>jython</code> and others may differ from one another and from CPython when it comes to their implementation specifics. To understand that better, <strong>it may help to understand that there is a difference between Python the semantics (the language) and the underlying implementation</strong></p> <blockquote> <p>Given this knowledge are there any recommendations on memory management for large number/data crunching?</p> </blockquote> <p>Now I can not speak about this, but I am sure that <a href="http://www.numpy.org/" rel="noreferrer">NumPy</a> (the most popular python library for number crunching) has mechanisms that handle memory consumption gracefully.</p> <p>If you would like to know more about Python's Internals take a look at these resources: </p> <ul> <li><a href="http://www.youtube.com/watch?v=XGF3Qu4dUqk" rel="noreferrer">Stepping through CPython</a> (video)</li> <li><a href="http://www.troeger.eu/files/teaching/pythonvm08.pdf" rel="noreferrer">A presentation about the internals of the Python Virtual Machine</a></li> <li>In true hacker spirit, the <a href="https://github.com/python/cpython/blob/master/Objects/obmalloc.c" rel="noreferrer">CPython Object Allocator source code</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload