Python vs Java garbage collection?

"Martin v. Löwis" martin at v.loewis.de
Mon Dec 23 07:59:14 EST 2002


Paul Foley wrote:
> But you can tell the difference between leaving it open and closing
> it.  If the "file" is a socket

But it isn't. The specific code in question was open(filename).read().
That you have to close a socket when you are done to perform graceful 
shutdown of the connection is out of question. Of course, there may be 
applications where even that is not necessary, e.g. if it is acceptable 
that the socket stays open as the program is short-lived, or as the 
other end will close the connection after some time of inactivity anyway.

> And if you can't tell the difference, why do you care when the
> finalization happens?  Instantly or 10 months later, it makes no
> difference.

Right. The only problem is that the resources aren't unlimited, and the 
number of open files is often quite limited. You will notice an 
exhaustion of the resource, so you want the system to reclaim unneeded 
resources before they contribute to exhaustion.

> [Also, I'd expect a finalizer for a database transaction, for example,
> to /abort/ the transaction, not commit it!  Don't you agree?

There should be always an option to explicitly release the resource; if 
there are different alternatives, refuse the temptation to guess.

Regards,
Martin




More information about the Python-list mailing list