@@ -413,66 +413,59 @@ How does Python manage memory?
413413------------------------------
414414
415415The details of Python memory management depend on the implementation. The
416- standard C implementation of Python uses reference counting to detect
417- inaccessible objects, and another mechanism to collect reference cycles,
416+ standard implementation of Python, :term: ` CPython `, uses reference counting to
417+ detect inaccessible objects, and another mechanism to collect reference cycles,
418418periodically executing a cycle detection algorithm which looks for inaccessible
419419cycles and deletes the objects involved. The :mod: `gc ` module provides functions
420420to perform a garbage collection, obtain debugging statistics, and tune the
421421collector's parameters.
422422
423- Jython relies on the Java runtime so the JVM's garbage collector is used. This
424- difference can cause some subtle porting problems if your Python code depends on
425- the behavior of the reference counting implementation.
423+ Other implementations (such as `Jython <http://www.jython.org >`_ or
424+ `PyPy <http://www.pypy.org >`_), however, can rely on a different mechanism
425+ such as a full-blown garbage collector. This difference can cause some
426+ subtle porting problems if your Python code depends on the behavior of the
427+ reference counting implementation.
426428
427- .. XXX relevant for Python 3?
429+ In some Python implementations, the following code (which is fine in CPython)
430+ will probably run out of file descriptors::
428431
429- Sometimes objects get stuck in traceback temporarily and hence are not
430- deallocated when you might expect. Clear the traceback with::
432+ for file in very_long_list_of_files:
433+ f = open(file)
434+ c = f.read(1)
435+
436+ Indeed, using CPython's reference counting and destructor scheme, each new
437+ assignment to *f * closes the previous file. With a traditional GC, however,
438+ those file objects will only get collected (and closed) at varying and possibly
439+ long intervals.
440+
441+ If you want to write code that will work with any Python implementation,
442+ you should explicitly close the file or use the :keyword: `with ` statement;
443+ this will work regardless of memory management scheme::
431444
432- import sys
433- sys.last_traceback = None
445+ for file in very_long_list_of_files:
446+ with open(file) as f:
447+ c = f.read(1)
434448
435- Tracebacks are used for reporting errors, implementing debuggers and related
436- things. They contain a portion of the program state extracted during the
437- handling of an exception (usually the most recent exception).
438449
439- In the absence of circularities, Python programs do not need to manage memory
440- explicitly.
450+ Why doesn't CPython use a more traditional garbage collection scheme?
451+ ---------------------------------------------------------------------
441452
442- Why doesn't Python use a more traditional garbage collection scheme? For one
443- thing, this is not a C standard feature and hence it's not portable. (Yes, we
444- know about the Boehm GC library. It has bits of assembler code for *most *
445- common platforms, not for all of them, and although it is mostly transparent, it
446- isn't completely transparent; patches are required to get Python to work with
447- it.)
453+ For one thing, this is not a C standard feature and hence it's not portable.
454+ (Yes, we know about the Boehm GC library. It has bits of assembler code for
455+ *most * common platforms, not for all of them, and although it is mostly
456+ transparent, it isn't completely transparent; patches are required to get
457+ Python to work with it.)
448458
449459Traditional GC also becomes a problem when Python is embedded into other
450460applications. While in a standalone Python it's fine to replace the standard
451461malloc() and free() with versions provided by the GC library, an application
452462embedding Python may want to have its *own * substitute for malloc() and free(),
453- and may not want Python's. Right now, Python works with anything that
463+ and may not want Python's. Right now, CPython works with anything that
454464implements malloc() and free() properly.
455465
456- In Jython, the following code (which is fine in CPython) will probably run out
457- of file descriptors long before it runs out of memory::
458-
459- for file in very_long_list_of_files:
460- f = open(file)
461- c = f.read(1)
462-
463- Using the current reference counting and destructor scheme, each new assignment
464- to f closes the previous file. Using GC, this is not guaranteed. If you want
465- to write code that will work with any Python implementation, you should
466- explicitly close the file or use the :keyword: `with ` statement; this will work
467- regardless of GC::
468-
469- for file in very_long_list_of_files:
470- with open(file) as f:
471- c = f.read(1)
472466
473-
474- Why isn't all memory freed when Python exits?
475- ---------------------------------------------
467+ Why isn't all memory freed when CPython exits?
468+ ----------------------------------------------
476469
477470Objects referenced from the global namespaces of Python modules are not always
478471deallocated when Python exits. This may happen if there are circular
0 commit comments