Re: Context manager for files vs garbage collection
- From: "Sebastian \"lunar\" Wiesner" <basti.wiesner@xxxxxxx>
- Date: Tue, 17 Jun 2008 19:24:06 +0200
Floris Bruynooghe <floris.bruynooghe@xxxxxxxxx>:
I was wondering when it was worthwil to use context managers for
file. Consider this example:
def foo():
t = False
for line in file('/tmp/foo'):
if line.startswith('bar'):
t = True
break
return t
Using this code inside a jthon web application might hit the resource limits
of the underlying operating system.
While CPython has a fairly deterministic garbage collector, the JVM gc is
non-deterministic, especially inside the server vm. It keeps objects
around for quite a long time and only frees them, if available memory runs
low or the application is idle. Since file objects don't consume much
memory, they might be hanging around for quite some time still claiming
resources, which are a rare thing on many restricted server environments.
Relying on garbage collection on Python means relying on a non-portable
implementation detail.
--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
.
- References:
- Context manager for files vs garbage collection
- From: Floris Bruynooghe
- Context manager for files vs garbage collection
- Prev by Date: Re: Numeric type conversions
- Next by Date: Re: Buffer size when receiving data through a socket?
- Previous by thread: Re: Context manager for files vs garbage collection
- Next by thread: Python GC does not work as it should be
- Index(es):
Relevant Pages
|