|
Original |
Translation |
|
87
|
Note that the main script executed by Python, even if its filename ends in .py, is not compiled to a .pyc file. It is compiled to bytecode, but the bytecode is not saved to a file. Usually main scripts are quite short, so this doesn't cost much speed.
|
|
|
88
|
There are also several programs which make it easier to intermingle Python and C code in various ways to increase performance. See, for example, `Psyco <http://psyco.sourceforge.net/>`_, `Pyrex <http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/>`_, `PyInline <http://pyinline.sourceforge.net/>`_, `Py2Cmod <http://sourceforge.net/projects/py2cmod/>`_, and `Weave <http://www.scipy.org/Weave>`_.
|
|
|
89
|
How does Python manage memory?
|
|
|
90
|
The details of Python memory management depend on the implementation. The standard C implementation of Python uses reference counting to detect inaccessible objects, and another mechanism to collect reference cycles, periodically executing a cycle detection algorithm which looks for inaccessible cycles and deletes the objects involved. The :mod:`gc` module provides functions to perform a garbage collection, obtain debugging statistics, and tune the collector's parameters.
|
|
|
91
|
|
92
|
In the absence of circularities, Python programs do not need to manage memory explicitly.
|
|
|
93
|
Why doesn't Python use a more traditional garbage collection scheme? For one thing, this is not a C standard feature and hence it's not portable. (Yes, we know about the Boehm GC library. It has bits of assembler code for *most* common platforms, not for all of them, and although it is mostly transparent, it isn't completely transparent; patches are required to get Python to work with it.)
|
|
|
94
|
Traditional GC also becomes a problem when Python is embedded into other applications. While in a standalone Python it's fine to replace the standard malloc() and free() with versions provided by the GC library, an application embedding Python may want to have its *own* substitute for malloc() and free(), and may not want Python's. Right now, Python works with anything that implements malloc() and free() properly.
|
|
|
95
|
In Jython, the following code (which is fine in CPython) will probably run out of file descriptors long before it runs out of memory::
|
|
|
96
|
for file in very_long_list_of_files: f = open(file) c = f.read(1)
|
|