Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>CPython <a href="http://hg.python.org/cpython/file/3d0686d90f55/Objects/obmalloc.c#l20" rel="nofollow">allocates small objects</a> (obmalloc.c, 3.2.3) out of 4 KiB pools that it manages in 256 KiB blocks called arenas. Each active pool has a fixed block size ranging from 8 bytes up to 256 bytes, in steps of 8. For example, a 14-byte object is allocated from the first available pool that has a 16-byte block size. </p> <p>There's a potential problem if arenas are allocated on the heap instead of using mmap (this is tunable via <a href="http://www.gnu.org/software/libc/manual/html_node/Malloc-Tunable-Parameters.html" rel="nofollow">mallopt's <code>M_MMAP_THRESHOLD</code></a>), in that the heap cannot shrink below the highest allocated arena, which will not be released so long as 1 block in 1 pool is allocated to an object (CPython doesn't float objects around in memory).</p> <p>Given the above, the following version of your function should probably solve the problem. Replace the line <code>return len(explored)</code> with the following 3 lines:</p> <pre><code> result = len(explored) del i, x, to_explore, current, explored return result + 0 </code></pre> <p>After deallocating the containers and all referenced objects (releasing arenas back to the system), this returns a new <code>int</code> with the expression <code>result + 0</code>. The heap cannot shrink as long as there's a reference to the first result object. In this case that gets automatically deallocated when the function returns.</p> <p>If you're testing this interactively without the "plus 0" step, remember that the REPL (Read, Eval, Print, Loop) keeps a reference to the last result accessible via the pseudo-variable "<code>_</code>". </p> <p>In Python 3.3 this shouldn't be an issue since the object allocator was modified to <a href="http://hg.python.org/cpython/file/bd8afb90ebf2/Objects/obmalloc.c#l595" rel="nofollow">use anonymous mmap for arenas</a>, where available. (The upper limit on the object allocator was also bumped to 512 bytes to accommodate 64-bit platforms, but that's inconsequential here.)</p> <p>Regarding manual garbage collection, <code>gc.collect()</code> does a full collection of tracked container objects, but it also <a href="http://hg.python.org/cpython/file/bd8afb90ebf2/Modules/gcmodule.c#l802" rel="nofollow">clears freelists</a> of objects that are maintained by built-in types (e.g. frames, methods, floats). Python 3.3 added additional API functions to clear freelists used by lists (<a href="http://docs.python.org/3/c-api/list.html#PyList_ClearFreeList" rel="nofollow"><code>PyList_ClearFreeList</code></a>), dicts (<a href="http://docs.python.org/3/c-api/dict.html#PyDict_ClearFreeList" rel="nofollow"><code>PyDict_ClearFreeList</code></a>), and sets (<a href="http://docs.python.org/3/c-api/set.html#PySet_ClearFreeList" rel="nofollow"><code>PySet_ClearFreeList</code></a>). If you'd prefer to keep the freelists intact, use <code>gc.collect(1)</code>.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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