# Open a file and never close it

**URL:** <https://discourse.julialang.org/t/open-a-file-and-never-close-it/19091>\
**Category:** General Usage\
**Tags:** question, garbage-collection, io\
**Created:** [December 29, 2018, 2:08pm UTC](https://discourse.julialang.org/t/open-a-file-and-never-close-it/19091 "2018-12-29T14:08:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [December 29, 2018, 2:08pm UTC](https://discourse.julialang.org/t/open-a-file-and-never-close-it/19091/1 "2018-12-29T14:08:51Z")

</div>

I know this is considered bad style, but I would like to better understand why. What happens if one opens a file and never closes it?

```julia
function sloppy()
     open("some_file", "w")
     nothing
end

sloppy()

```

Will the `GC` close this automatically? In what problems could I run, if I stop closing files?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 29, 2018, 2:14pm UTC](https://discourse.julialang.org/t/open-a-file-and-never-close-it/19091/2 "2018-12-29T14:14:10Z")

</div>

> [@jw3126](#):
>
> Will the `GC` close this automatically?

Yes, it will, once the file handle object is collected (or when the process terminates).

> In what problems could I run, if I stop closing files?

There are maximum file handle limits and you can be unable to open any more files if you’ve opened too many and not closed them. It’s also possible to have written data to a file and have that not be flushed until the file handle is closed. You can call `flush` to do that explicitly but if you’re not bothering to close a file then presumably you’re not doing that either.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 29, 2018, 2:15pm UTC](https://discourse.julialang.org/t/open-a-file-and-never-close-it/19091/3 "2018-12-29T14:15:29Z")

</div>

Oh, and on Windows if you have a file open then it can’t be deleted by anyone and probably various other annoying limitations.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 29, 2018, 2:15pm UTC](https://discourse.julialang.org/t/open-a-file-and-never-close-it/19091/4 "2018-12-29T14:15:38Z")

</div>

> [@jw3126](#):
>
> In what problems could I run, if I stop closing files?

```julia
julia> fn = tempname()
"/tmp/julia3mUCjj"

julia> touch(fn)
"/tmp/julia3mUCjj"

julia> manyio = [open(fn, "r") for _ in 1:10000]
ERROR: SystemError: opening file "/tmp/julia3mUCjj": Too many open files
Stacktrace:
 [1] #systemerror#43(::Nothing, ::Function, ::String, ::Bool) at ./error.jl:134
 [2] systemerror at ./error.jl:134 [inlined]
 [3] #open#309(::Bool, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Function, ::String) at ./iostream.jl:283
 [4] #open at ./none:0 [inlined]
 [5] open(::String, ::String) at ./iostream.jl:339
 [6] (::getfield(Main, Symbol("##3#4")))(::Int64) at ./none:0
 [7] iterate at ./generator.jl:47 [inlined]
 [8] collect_to!(::Array{IOStream,1}, ::Base.Generator{UnitRange{Int64},getfield(Main, Symbol("##3#4"))}, ::Int64, ::Int64) at ./array.jl:650
 [9] collect_to_with_first!(::Array{IOStream,1}, ::IOStream, ::Base.Generator{UnitRange{Int64},getfield(Main, Symbol("##3#4"))}, ::Int64) at ./array.jl:629
 [10] collect(::Base.Generator{UnitRange{Int64},getfield(Main, Symbol("##3#4"))}) at ./array.jl:610
 [11] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [April 21, 2022, 11:52am UTC](https://discourse.julialang.org/t/open-a-file-and-never-close-it/19091/5 "2022-04-21T11:52:17Z")

</div>

> [@StefanKarpinski](#):
>
> > [@](#):
> >
> > Will the `GC` close this automatically?
> 
> Yes, it will, once the file handle object is collected (or when the process terminates).

I was thinking is there a quicker way to terminate a process (if so it could be Julia’s default)? It doesn’t seem we need to deallocate all memory (non-GC languages wouldn’t always; I’m thinking of benchmarks, to compete with them).

If closing files is actually triggered by the GC, or finalizers, then they would be left out. Since all the file handles of a program are known, it seems they could be flushed and closed by Julia on `exit()`. I believe the kernel (in all modern operating systems) will close the files; but will not, even can’t (fully) flush?

> [@StefanKarpinski](#):
>
> on Windows if you have a file open then it can’t be deleted by anyone and probably various other annoying limitations.

Right, known, and unrelated to Julia. And all those problem will go away when closed [by Julia].
