Do I need to close a FileStream?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Do I need to close a FileStream?

Tim Johnson-6
Hi,

Do I need to worry about closing a read-only FileStream when I am  
done reading from it?

What happens if I don't?

How can I tell if I have lots of open files in my system?  
"FileStream allInstances inspect"?

Thanks,
Tim

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do I need to close a FileStream?

Bert Freudenberg
On Mar 24, 2007, at 8:34 , Tim Johnson wrote:

> Hi,
>
> Do I need to worry about closing a read-only FileStream when I am  
> done reading from it?

Yes. Always. A common pattern is

        f := ... open the file stream ....
        [ ... do something with f ... ] ensure: [f close]

This ensures f is closed even if there is an error in your processing  
code.

> What happens if I don't?

Bad Things ;) I'm not exactly sure.

> How can I tell if I have lots of open files in my system?  
> "FileStream allInstances inspect"?

You would need to use #allSubInstances and check if they're open.

- Bert -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do I need to close a FileStream?

Kyle Hamilton
The underlying OS has only a certain number of file handles that it
can maintain open at a time.  On UNIX-like systems, there is also
possibly a maximum number of files per process.  If this limit is hit,
then no additional files can be opened, and on any attempt to do so an
error is returned.

-Kyle H

On 3/24/07, Bert Freudenberg <[hidden email]> wrote:

> On Mar 24, 2007, at 8:34 , Tim Johnson wrote:
>
> > Hi,
> >
> > Do I need to worry about closing a read-only FileStream when I am
> > done reading from it?
>
> Yes. Always. A common pattern is
>
>         f := ... open the file stream ....
>         [ ... do something with f ... ] ensure: [f close]
>
> This ensures f is closed even if there is an error in your processing
> code.
>
> > What happens if I don't?
>
> Bad Things ;) I'm not exactly sure.
>
> > How can I tell if I have lots of open files in my system?
> > "FileStream allInstances inspect"?
>
> You would need to use #allSubInstances and check if they're open.
>
> - Bert -
>
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>


--

-Kyle H
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do I need to close a FileStream?

Tim Johnson-6
In reply to this post by Bert Freudenberg
Thanks!

- Tim

On Mar 24, 2007, at 3:25 AM, Bert Freudenberg wrote:

> On Mar 24, 2007, at 8:34 , Tim Johnson wrote:
>
>> Hi,
>>
>> Do I need to worry about closing a read-only FileStream when I am  
>> done reading from it?
>
> Yes. Always. A common pattern is
>
> f := ... open the file stream ....
> [ ... do something with f ... ] ensure: [f close]
>
> This ensures f is closed even if there is an error in your  
> processing code.
>
>> What happens if I don't?
>
> Bad Things ;) I'm not exactly sure.
>
>> How can I tell if I have lots of open files in my system?  
>> "FileStream allInstances inspect"?
>
> You would need to use #allSubInstances and check if they're open.
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do I need to close a FileStream?

Tim Johnson-6
In reply to this post by Kyle Hamilton

On Mar 24, 2007, at 3:45 AM, Kyle Hamilton wrote:

The underlying OS has only a certain number of file handles that it

can maintain open at a time.  On UNIX-like systems, there is also

possibly a maximum number of files per process.  If this limit is hit,

then no additional files can be opened, and on any attempt to do so an

error is returned.


For some reason I was confused and thought that Squeak's garbage collector would somehow make me immune from this.  Like when the file was no longer being used, it would be closed and purged.  Now I know otherwise :)  

Thanks,
Tim



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do I need to close a FileStream?

Bert Freudenberg

On Mar 24, 2007, at 17:41 , Tim Johnson wrote:

>
> On Mar 24, 2007, at 3:45 AM, Kyle Hamilton wrote:
>
>> The underlying OS has only a certain number of file handles that it
>> can maintain open at a time.  On UNIX-like systems, there is also
>> possibly a maximum number of files per process.  If this limit is  
>> hit,
>> then no additional files can be opened, and on any attempt to do  
>> so an
>> error is returned.
>
> For some reason I was confused and thought that Squeak's garbage  
> collector would somehow make me immune from this.  Like when the  
> file was no longer being used, it would be closed and purged.

Actually it is indeed. We use finalization for this [*]. However, you  
cannot know _when_ an object actually will be finalized, so you may  
be eating up handles nontheless.

> Now I know otherwise :)

It's good practice not to rely on finalization, but treat it as a  
safety net.

- Bert -

[*] See for example
http://www.mail-archive.com/beginners@.../ 
msg01719.html
or
http://www.google.com/search?q=squeak+finalization
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do I need to close a FileStream?

Kyle Hamilton
I think that some sort of diagnostic code printed out whenever an
object's finalization routine must change the internal state of the
object such that the criteria for finalization can be met would be a
good thing.

i.e., if a FileStream is holding a file open, the finalization code
should check to see that the file is closed.  If it is not, it should
warn about changing that state during the finalization.

Any finalization-related diagnostics should be treated the same way
that other languages call "leaks", and quashed, regardless.

-Kyle H

On 3/25/07, Bert Freudenberg <[hidden email]> wrote:

>
> On Mar 24, 2007, at 17:41 , Tim Johnson wrote:
>
> >
> > On Mar 24, 2007, at 3:45 AM, Kyle Hamilton wrote:
> >
> >> The underlying OS has only a certain number of file handles that it
> >> can maintain open at a time.  On UNIX-like systems, there is also
> >> possibly a maximum number of files per process.  If this limit is
> >> hit,
> >> then no additional files can be opened, and on any attempt to do
> >> so an
> >> error is returned.
> >
> > For some reason I was confused and thought that Squeak's garbage
> > collector would somehow make me immune from this.  Like when the
> > file was no longer being used, it would be closed and purged.
>
> Actually it is indeed. We use finalization for this [*]. However, you
> cannot know _when_ an object actually will be finalized, so you may
> be eating up handles nontheless.
>
> > Now I know otherwise :)
>
> It's good practice not to rely on finalization, but treat it as a
> safety net.
>
> - Bert -
>
> [*] See for example
> http://www.mail-archive.com/beginners@.../
> msg01719.html
> or
> http://www.google.com/search?q=squeak+finalization
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>


--

-Kyle H
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners