# Plots/GR + png() results in "Too many open files"

**URL:** <https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998>\
**Category:** General Usage\
**Created:** [October 25, 2020, 7:36pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998 "2020-10-25T19:36:46Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![ErikE](https://avatars.discourse-cdn.com/v4/letter/e/e9a140/32.png) [@ErikE](https://discourse.julialang.org/u/ErikE)\
**Post date:** [October 25, 2020, 7:36pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/1 "2020-10-25T19:36:46Z")

</div>

Hello!

I have a piece of Julia code where I generate (a lot of) scatter plots with the scatter function from Plots (with the GR backend), and save the plots to PNGs, using the png(filename) function (to later turn them into an animation using apngasm). The problem I’m facing is that after a while, my code crashes with

> socket: Too many open files  
> GKS: can’t connect to GKS socket application
> 
> GKS: Open failed in routine OPEN\_WS  
> GKS: GKS not in proper state. GKS must be either in the state WSOP or WSAC in routine ACTIVATE\_WS  
> ERROR: LoadError: SystemError: opening file “/tmp/jl\_RcjyRH.png”: No such file or directory  
> Stacktrace:  
> [1] systemerror(::String, ::Int32; extrainfo::Nothing) at ./error.jl:168  
> [2] #systemerror#48 at ./error.jl:167 [inlined]  
> [3] systemerror at ./error.jl:167 [inlined]  
> [4] open(::String; lock::Bool, read::Nothing, write::Nothing, create::Nothing, truncate::Nothing, append::Nothing) at ./iostream.jl:284  
> [5] open at ./iostream.jl:273 [inlined]  
> [6] open(::Base.var"#296#297"{String}, ::String; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at ./io.jl:323  
> [7] open at ./io.jl:323 [inlined]  
> [8] read at ./io.jl:410 [inlined]  
> [9] \_show(::IOStream, ::MIME{Symbol(“image/png”)}, ::Plots.Plot{Plots.GRBackend}) at /home/erik/.julia/packages/Plots/hyS17/src/backends/gr.jl:1780  
> [10] show at /home/erik/.julia/packages/Plots/hyS17/src/output.jl:215 [inlined]  
> [11] png(::Plots.Plot{Plots.GRBackend}, ::String) at /home/erik/.julia/packages/Plots/hyS17/src/output.jl:7  
> [12] png(::String) at /home/erik/.julia/packages/Plots/hyS17/src/output.jl:10  
> .  
> .  
> .

I presume either the \*.png files I’m writing, or some temporary files being used in the processes, are opened, but never closed. Is there something I could/should do to close those files once I’m done writing them?

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [October 25, 2020, 9:13pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/2 "2020-10-25T21:13:54Z")

</div>

Which GR version are you using?

```julia
using GR
version()
] st GR

```

---

<div class="post-metadata">

**Author:** ![ErikE](https://avatars.discourse-cdn.com/v4/letter/e/e9a140/32.png) [@ErikE](https://discourse.julialang.org/u/ErikE)\
**Post date:** [October 25, 2020, 9:32pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/3 "2020-10-25T21:32:17Z")

</div>

I seem to be using version “0.52.0”.

Here’s a small test program to demonstrate the problem:

```julia
using Plots
using Printf

ENV["QT_QPA_PLATFORM"] = "offscreen"

gr()

for i = 1:2000
    scatter(rand(5), rand(5))
    fn = @sprintf("hepp-%4.4i.png", i)
    println(i)
    png(fn)
    sleep(0.1)
end

```

Running it results in output like

```julia
.
.
.
1024
1025
1026
1027
socket: Too many open files
GKS: can't connect to GKS socket application

GKS: Open failed in routine OPEN_WS
GKS: GKS not in proper state. GKS must be either in the state WSOP or WSAC in routine ACTIVATE_WS
ERROR: LoadError: SystemError: opening file "/tmp/jl_bgLbsZ.png": No such file or directory

```

Notes:

- The exact number of png files I can generate before the crash varies a bit, but it’s usually a little bit more than 1000.
- If I don’t put the `sleep(0.1)` there, I get a lot of

```julia
read: Connection reset by peer
send: Broken pipe

```

That, however, was not required in my “real” program. Possibly because it generates bigger plots, so it’s a bit slower (maybe `png()` doesn’t like to be called too often?)

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [October 30, 2020, 9:28am UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/4 "2020-10-30T09:28:59Z")

</div>

I could not reproduce this behaviour on macOS. Which OS are you using?

---

<div class="post-metadata">

**Author:** ![ErikE](https://avatars.discourse-cdn.com/v4/letter/e/e9a140/32.png) [@ErikE](https://discourse.julialang.org/u/ErikE)\
**Post date:** [October 30, 2020, 12:15pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/5 "2020-10-30T12:15:09Z")

</div>

Linux. Ubuntu 20.04 to be precise.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [October 30, 2020, 2:53pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/6 "2020-10-30T14:53:09Z")

</div>

I can confirm this, on Ubuntu 20.05, clean installation of Julia v1.5.2, Plots and GR:

```julia
[...]
292
293
294
read: Connection reset by peer
send: Broken pipe
295
GKSserver: Failed to listen to port 8410
read: Connection reset by peer
send: Broken pipe
296
297
read: Connection reset by peer
send: Broken pipe
298
299
300
[...]

```

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [October 30, 2020, 2:54pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/7 "2020-10-30T14:54:29Z")

</div>

Same happens in a CentOS-7 container, so I don’t think it’s distribution-specific.

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [October 30, 2020, 3:29pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/8 "2020-10-30T15:29:17Z")

</div>

This is a different issue. Relates to the socket connection - the required port isn’t free’d in time.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [October 30, 2020, 4:07pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/9 "2020-10-30T16:07:19Z")

</div>

Ah, right, of course!

---

<div class="post-metadata">

**Author:** ![ErikE](https://avatars.discourse-cdn.com/v4/letter/e/e9a140/32.png) [@ErikE](https://discourse.julialang.org/u/ErikE)\
**Post date:** [October 30, 2020, 8:05pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/10 "2020-10-30T20:05:42Z")

</div>

One important factor here is

```julia
ulimit -a | grep "open file"
open files (-n) 1024

```

If I do `ulimit -n 2048` the problem is solved - sort of. I feel there is hardly any reason why so many files would need to be kept open all at the same time. Once it’s done writing a file, the file should be closed.

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [October 31, 2020, 4:33pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/11 "2020-10-31T16:33:25Z")

</div>

In the meantime I could reproduce the problem on Ubuntu 20.04. The point is, that due to the plugin setting (`QT_QPA_PLATFORM`) the `GKS QtTerm` socket is never closed.

If you want to produce the PNGs only, then it’s much easier to use the following script:

```julia
using Plots
using Printf

ENV["GKSwstype"] = "100"

gr()

for i = 1:2000
    scatter(rand(5), rand(5))
    fn = @sprintf("hepp-%4.4i.png", i)
    println(i)
    png(fn)
end

```

---

<div class="post-metadata">

**Author:** ![ErikE](https://avatars.discourse-cdn.com/v4/letter/e/e9a140/32.png) [@ErikE](https://discourse.julialang.org/u/ErikE)\
**Post date:** [October 31, 2020, 5:06pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/12 "2020-10-31T17:06:31Z")

</div>

Aha! Yes, that works. And, as an added bonus, it’s a bit faster as well 🙂

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![sajad\_ghassemi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajad_ghassemi/32/25241_2.png) [@sajad\_ghassemi](https://discourse.julialang.org/u/sajad_ghassemi)\
**Post date:** [May 20, 2021, 11:02pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/13 "2021-05-20T23:02:30Z")

</div>

> [@jheinen](#):
>
> `using GR`

when I used the using GR  
WARNING: using GR.plot in module Main conflicts with an existing identifier.  
what should I do ? I asked in a seperate question about my problem but none answered me:(

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [May 21, 2021, 2:09am UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/14 "2021-05-21T02:09:35Z")

</div>

Both `Plots` and `GR` export commands like `plot`. So you can only use one of them: `using Plots` (with the `GR` backend) or `using GR`.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [May 21, 2021, 6:04am UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/15 "2021-05-21T06:04:12Z")

</div>

> [@jheinen](#):
>
> So you can only use one of them: `using Plots` (with the `GR` backend) or `using GR` .

If you need to access the commands, do `import Plots` or `import GR` instead. If you `import GR`, then you’ll have to use `GR.plot` to explicitly refer to the plot method in the `GR` module.

---

<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:** [June 8, 2021, 5:42pm UTC](https://discourse.julialang.org/t/plots-gr-png-results-in-too-many-open-files/48998/16 "2021-06-08T17:42:11Z")

</div>

> [@ErikE](#):
>
> If I do `ulimit -n 2048` the problem is solved - sort of. I feel there is hardly any reason why so many files would need to be kept open all at the same time. Once it’s done writing a file, the file should be closed

Right, and that said, if it isn’t already fixed (reading here it seemed so), while you can use ulimit, there’s an outstanding PR:

> <https://github.com/JuliaLang/julia/pull/41044>
>
> closes #40891 .

With another just opened PR: “Set VERSION to 1.8.0-DEV, move 1.7 NEWS to HISTORY” I’m not sure if the above one made it in, since it seems very harmless and only potentially helpful, I would like to see it in, even if it would only make buggy software less of an issue. Possibly it will come in 1.7.x? I’m not sure were they draw the line at adding stuff in such a release.
