Re: Re: Re: my garbage collector sucks
Joe Mason <[email protected]>
| Newsgroups | gmane.comp.lang.prothon.user |
|---|---|
| Message-ID | <[email protected]> |
On Tue, Jun 29, 2004 at 08:26:37AM -0700, Paul Prescod wrote:
> The Halting Problem only applies to *computer programs* trying to
> predict the behaviour of other *computer programs*. I am talking about a
> human being trying to predict the behaviour of a computer program.
> Anyhow, this is blindingly obvious if you write the following Python
> program:
>
> for i in range(0, 10000):
> file = open(str(i)+".txt", "w").write("Hello world")
>
> Maybe there is no *computer program* that can see that this would have
> different behaviour in Python versus Jython/Java but I can see it with
> my own two eyes. In Jython it will open somewhere between 1 and 10000
> files simultaneously and in Python there will never be more than two
> files open at a time.
You are confusing memory allocation with resource allocation. The
garbage collector, be it refcounting or mark-and-sweep, only reclaims
memory - it is up to you to deal with file handles and similar
resources.
Your program will open thousands of files in Jython because it's buggy:
it never calls file.close(). That it happens to work better on Python
is an implementation detail.
(It's my opinion you shouldn't ever implement finalizers - if there's
some resource that hasn't been freed when the memory is reclaimed,
that's a BUG, because you were supposed to free it earlier. Maybe a
finalizer that throws an exception if not all resources have been closed
would be ok...)
Yes, this means you still have to manually track a whole bunch of
things, even with GC. That's life.
Joe