# Multithreaded IOBuffer memory leak?

**URL:** <https://discourse.julialang.org/t/multithreaded-iobuffer-memory-leak/98003>\
**Category:** General Usage\
**Created:** [April 27, 2023, 4:37pm UTC](https://discourse.julialang.org/t/multithreaded-iobuffer-memory-leak/98003 "2023-04-27T16:37:29Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![louisponet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/louisponet/32/2070_2.png) [@louisponet](https://discourse.julialang.org/u/louisponet)\
**Post date:** [April 27, 2023, 9:31pm UTC](https://discourse.julialang.org/t/multithreaded-iobuffer-memory-leak/98003/4 "2023-04-27T21:31:15Z")

</div>

I’ve done some more digging running the following code, which should not be suffering from the fork bomb with while true, and is thus closer to my real world case:

```julia
function meminfo_julia!(d)
  push!(d["GC Live"], Base.gc_live_bytes()/2^20)
  push!(d["Max. RSS"], Sys.maxrss()/2^20)
end

function meminfo_procfs(d)
    pid=getpid()
  smaps = "/proc/$pid/smaps_rollup"
  if !isfile(smaps)
    error("`$smaps` not found. Maybe you are using an OS without procfs support or with an old kernel.")
  end

  rss = pss = shared = private = 0
  for line in eachline(smaps)
    s = split(line)
    if s[1] == "Rss:"
      rss += parse(Int64, s[2])
    elseif s[1] == "Pss:"
      pss += parse(Int64, s[2])
    end
  end

  push!(d["RSS"], rss/2^10)
  push!(d["PSS"], pss/2^10)
end

function explode(buffer = IOBuffer())
    lck = ReentrantLock()
    path = "test.log"
    @sync for _ in 1:1000000
        Threads.@spawn begin
            lock(lck) do
                println(buffer, "blabla")
                open(path, append=true) do f
                    write(f, take!(buffer))
                end
            end
        end
    end
end

memdict = Dict("GC Live" => Float64[],
               "Max. RSS" => Float64[],
               "RSS" => Float64[],
               "PSS" => Float64[])
t = Threads.@spawn let
    buffer = IOBuffer()
    for i = 1:200
        explode(buffer)
        meminfo_julia!(memdict)
        meminfo_procfs(memdict)
    end
end

```

Which takes ages, but produces:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/3/83d53eddce72aa2eb455abe641a23706f28a5244.png)

So while the memory consumption seems to stabilize around 6Gb, this is actually not quite true as it keeps trending upwards slowly but surely. I think this is exactly the leak I’m seeing in my real world case.

I’d love to get some more insights on what might be going on here

---

_[View the full topic](https://discourse.julialang.org/t/multithreaded-iobuffer-memory-leak/98003)._
