elvanja

elvanja

Binaries memory leak with quantum and (open)telemetry

A Phoenix app I am working on is experiencing binaries related memory leak during Quantum triggered processing. This is the gist of it:

  • a long running process is started with the app, given it does the work using a pool of workers
  • Quantum triggers the processing via GenServer.cast(:start_processing)
  • workers fetch data from external service, via JSON API
  • then parse data and store result in DB

The result is pretty much in line with findings in Memory leak with quantum and processing binary files. I tried the strings: :copy idea, but it did not work. Also tried hibernating related process (including poolboy workers), as suggested in Memory leaking with long running processes - #9 by dom, but binaries are still leaking.

Things tried without result:

  • pass strings: :copy to Jason, via plug(Tesla.Middleware.JSON, engine_opts: [strings: :copy]) (we use Tesla for the HTTP client)
  • do :hibernate in related processes (and workers), although this was added only to final handle_* callbacks, e.g. only those callbacks where we actually know the work is completed (not all)

Then I did some investigation to see which processes were actually hanging on to binaries after all this, and found them all to be telemetry related. Here’s the findings:

processes_with_large_binaries =
  Process.list()
  |> Enum.map(fn pid -> {pid, Process.info(pid, :binary)} end)
  |> Enum.reject(fn {pid, binaries} -> match?({:binary, []}, binaries) end)
  |> Enum.map(fn {pid, {:binary, binaries}} -> {pid, Enum.map(binaries, fn {_, size, _} -> size end)
  |> Enum.sum()} end)
  |> Enum.sort_by(fn {_, s} -> s end, &>=/2)

binary_largest = pids_with_details |> Enum.sort_by(& &1.binaries_size, :desc) |> Enum.take(10)

All seem to be telemetry related:

Enum.map(binary_largest, & &1.process_info[:dictionary])
[
  [
    "$initial_call": {:h2_stream, :init, 1},
    "$ancestors": [#PID<0.5412.0>, #PID<0.4567.0>, #PID<0.4550.0>,
     :otel_batch_processor, :opentelemetry_sup, #PID<0.4474.0>]
  ],
  [
    "$initial_call": {:h2_stream, :init, 1},
    "$ancestors": [#PID<0.5412.0>, #PID<0.4567.0>, #PID<0.4550.0>,
     :otel_batch_processor, :opentelemetry_sup, #PID<0.4474.0>]
  ],
  [
    "$initial_call": {:h2_stream, :init, 1},
    "$ancestors": [#PID<0.5412.0>, #PID<0.4567.0>, #PID<0.4550.0>,
     :otel_batch_processor, :opentelemetry_sup, #PID<0.4474.0>]
  ],
  [
    "$initial_call": {:h2_stream, :init, 1},
    "$ancestors": [#PID<0.5412.0>, #PID<0.4567.0>, #PID<0.4550.0>,
     :otel_batch_processor, :opentelemetry_sup, #PID<0.4474.0>]
  ],
  [
    "$initial_call": {:h2_stream, :init, 1},
    "$ancestors": [#PID<0.5412.0>, #PID<0.4567.0>, #PID<0.4550.0>,
     :otel_batch_processor, :opentelemetry_sup, #PID<0.4474.0>]
  ],
  [
    "$initial_call": {:h2_stream, :init, 1},
    "$ancestors": [#PID<0.5412.0>, #PID<0.4567.0>, #PID<0.4550.0>,
     :otel_batch_processor, :opentelemetry_sup, #PID<0.4474.0>]
  ],
  [
    "$initial_call": {:h2_stream, :init, 1},
    "$ancestors": [#PID<0.5412.0>, #PID<0.4567.0>, #PID<0.4550.0>,
     :otel_batch_processor, :opentelemetry_sup, #PID<0.4474.0>]
  ],
  [
    "$initial_call": {:h2_stream, :init, 1},
    "$ancestors": [#PID<0.5412.0>, #PID<0.4567.0>, #PID<0.4550.0>,
     :otel_batch_processor, :opentelemetry_sup, #PID<0.4474.0>]
  ],
  [
    "$initial_call": {:h2_stream, :init, 1},
    "$ancestors": [#PID<0.5412.0>, #PID<0.4567.0>, #PID<0.4550.0>,
     :otel_batch_processor, :opentelemetry_sup, #PID<0.4474.0>]
  ],
  [
    "$initial_call": {:h2_stream, :init, 1},
    "$ancestors": [#PID<0.5412.0>, #PID<0.4567.0>, #PID<0.4550.0>,
     :otel_batch_processor, :opentelemetry_sup, #PID<0.4474.0>]
  ]
]

A sample:

pid_with_details = Enum.random(binary_largest)
%{
  binaries_size: 977410683,
  memory: 196896,
  pid: #PID<0.7970.48>,
  process_info: [
    current_function: {:gen_statem, :loop_receive, 3},
    initial_call: {:proc_lib, :init_p, 5},
    status: :waiting,
    message_queue_len: 0,
    links: [#PID<0.5412.0>],
    dictionary: [
      "$initial_call": {:h2_stream, :init, 1},
      "$ancestors": [#PID<0.5412.0>, #PID<0.4567.0>, #PID<0.4550.0>,
       :otel_batch_processor, :opentelemetry_sup, #PID<0.4474.0>]
    ],
    trap_exit: true,
    error_handler: :error_handler,
    priority: :normal,
    group_leader: #PID<0.4473.0>,
    total_heap_size: 24503,
    heap_size: 6772,
    stack_size: 11,
    reductions: 93231,
    garbage_collection: [
      max_heap_size: %{error_logger: true, kill: true, size: 0},
      min_bin_vheap_size: 46422,
      min_heap_size: 233,
      fullsweep_after: 65535,
      minor_gcs: 10
    ],
    suspending: []
  ]
}

Before garbage collection:

Process.info(pid_with_details.pid, :binary)
{:binary,
 [
   {139802410680360, 6147237, 160},
   {139802410680360, 6147237, 160},
   {139802410680360, 6147237, 160},
   {139802410680360, 6147237, 160},
   {139802410680360, 6147237, 160},
   {139802410680360, 6147237, ...},
   {139802410680360, ...},
   {...},
   ...
 ]}

After garbage collecting:

:erlang.garbage_collect(pid_with_details.pid)
true

Process.info(pid_with_details.pid, :binary)
{:binary, []}

So even though the processes responsible for actual work don’t hold on to those binaries (at least I could not detect this), telemetry processes do hold on for some reason.

I am thinking of trying:

  • doing the :erlang.garbage_collect() in related processes and workers; but not sure if it makes sense to garbage collect them since binaries are held by telemetry processes
  • do a periodic scan for processes with large binaries, like above, and garbage collect them, maybe even trigger that from actual processes that do the work once they know they are done
  • make use of dynamic supervisor; but not sure how to build such a tree and still use pool of workers and likely this would not help given binaries are held in telemetry processes

But, given strings: :copy was used for Jason and those worker processes don’t seem to hold on to binaries, don’t think any solution to improve workers related code will help. And I would like to avoid garbage collection, seems like a sledge hammer solution to the problem.

Thoughts/ideas welcome!

Versions of libs:

  • telemetry 1.1.0
  • telemetry_metrics 0.6.0
  • telemetry_poller 0.5.1
  • opentelemetry 1.0.5
  • opentelemetry_api 1.0.3
  • opentelemetry_exporter 1.0.4
  • opentelemetry_telemetry 1.0.0
  • phoenix 1.5.8
  • jason 1.3.0

Marked As Solved

elvanja

elvanja

Seems that switching to :http_protobuf solves the memory leak problem, at least for my case. Details in Performance issue / holding reference to binaries · Issue #459 · open-telemetry/opentelemetry-erlang · GitHub.

Also Liked

tristan

tristan

Rebar3 Core Team

Just to have a response here as well, I believe this is the same issue as Binary leaking in h2_connection processes · Issue #120 · joedevivo/chatterbox · GitHub that sadly never got fixed but I believe I know how to.

I’ll update when I have added the patches, first a hibernate and then a full fix of not keeping around the body when it isn’t needed.

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement