Python 2.0

Paul Boddie paulb at infercor.no
Tue Jun 1 05:28:53 EDT 1999


Yukihiro Matsumoto wrote:
> 
> "Vadim Chugunov" <chega_ at yahoo.com> writes:

[...]

> |I'm sorry for being unclear, but the key words here were: "to free up
> |resources".
> |And I meant "to free up resources the moment I expect them to", not "some
> |time later".
> 
> Well, OK.  I understand now.  Now only thing I can say is, why do you
> emphasize so much on reclaiming object at the time you exepcted?

Because, and I have seen this happen in real-life situations, there's no
guarantee that as the last reference to an object is released all the resources
associated with that object are going to be released at that time. Now, if one
has some kind of object containing a reference to a socket object (for
simplicity let us say that this is a basic resource), then it means that the
socket may for an indefinite amount of time be floating around in a reserved,
but unusable state. Now, if the garbage collector doesn't get around to this
object before one instantiates more of them it is possible that one runs out of
resources: too many sockets in use.

As has been discussed before on this newsgroup/list, such resources get
overlooked in many a garbage collection scheme. I'm sure a perusal of various
news/list archives would provide a substantial amount of relevant literature.

> Because of your C++ background?

It is amusing to note that in (probably most implementations of) Java one has to
write code as one would in C++ in situations like that outlined above, using
explicit destructor-like "close" methods on all resource objects. It isn't
sufficient to let the last reference slip away through one's fingers with a
happy feeling. ;-)

> |BTW, this is a common expectation from anyone coming from a C++ background.
> |In C++, deallocation in destructors is the only way you can use exceptions
> |without leaking resources.
> 
> Yes, but it's not true for HIGHER languages like Python or Java (or Ruby).

Absolutely, as far as memory is concerned, but for resources there is a kind of
temporary "resource leak" which is critical, and about which the collector
doesn't apparently care.

Paul




More information about the Python-list mailing list