Fl4m3Ph03n1x

Fl4m3Ph03n1x

Background

We have an application where processes memory keeps rising, continuously, without end until the machine crashes. We have no clue why this is happening and we need ideas for possible causes so we can research in more detail.

Research

Our first approach was to check the processes State. These are workers, so we naturally assumed that the processe’s State was growing without bound. After checking several processes, we concluded that their state was not big enough to occupy 30 MB nor did it grow without boundary.

Then we shifted our attention to process number. Perhaps we were creating processes without stopping. No such thing either.

Then we moved our attention to garbage collection. Turns out that if we issued an major GC on all our worker processes, RAM usage would go down immediately. So we started issues periodic major GC on our GenServers via :erlang.garbage_collect() but the problem somehow persists.

Our tool of election (observer_cli) shows that the issue is clearly in process memory, but with these options of of the way I can’t think of anything else.

Brainstorming

Does anyone have any idea on why or what could be causing process memory to go up without and end? Any guides on memory leaks would be welcome, we focused our attention in “Erlang in Anger” but to no avail thus far.

Showing Posts 1 to 10

idi527

idi527

What have you actually tried to find the problematic processes? Have you tried listing all processes, checking their memory?

JeyHey

JeyHey

Could be related to binaries not garbage collected. Sounds like the same problem as here: https://stackoverflow.com/questions/43613027/solving-large-binaries-leak/43685158

chasers

chasers

It’s not binary if observer shows it as process memory.

Been playing with this from wobserver: logflare/lib/logflare/system_metrics/wobserver/processes.ex at master · Logflare/logflare · GitHub

Check per process memory usage. Also mailbox length.

If it’s not obvious that’s is one or a small group of processes then track process count over time. Maybe it’s a lot of smaller procs building up.

sneako

sneako

Is there a crash dump being produced when the system crashes? If so, you might find some clues if you load the dump with the crash dump viewer Crashdump Viewer — OTP 29.0.2 (observer 2.19) and if not you should try enabling them.

I was able to pin point a bottleneck on my application just last week using this technique, when I noticed I had a task supervisor with many messages in it’s mailbox at the time of the crash.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

We know which processes are problematic. They belong to a group we call Workers. We identified this by checking their memory usage going up using observer_cli. It is evident that their memory grows continuously up to 30MB each, and because we have thousands of Worker processes, the machine eventually runs out of memory and crashes.

Per process memory usage keeps rising without end, but the mailboxes are empty.

It’s not process count. We have eliminated that possibility as the number of processes in the system is stable.

How do I enable a crashdump if the machine crashes while out of memory?
This technique could prove useful in deed !

sneako

sneako

The application can still produce the crash dump if it OOMs, in fact, the first couple of crash dump “slogans” documented in the link I shared earlier are exactly for that situation.
Unless you have disabled crash dumps or your server does not have a writable writable file system mounted, they should get written to the current working directory, for example I found the erl_crash.dump file sitting at the top of my release directory, right next to the bin dir. Maybe you already have one on one of your servers too. There are some env vars you can use to configure your crash dumps, documented here:

idi527

idi527

And what do they do?

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

These workers hold a State with a connection pid (which is in reality the PID from a gun process) and a map with streams (when making requests with gun, each request is a Stream )

Following is the state of each worker:

defmodule State do
    @moduledoc """
    State of the Worker. Most importantly, it saves a map of stream refs to
    URLs, so we know which URLs were affected when a stream breaks.
    """
    defstruct conn_pid: nil,
      active_streams: %{}
end

Now, what could possibly go wrong?

The only thing that goes through my mind is that the active_streams map may grow forever if a stream never receives the :fin_packet necessary to tell our worker the request is over (HTTP Protocol details).

But we have no reason to believe such is happening…

Is there a way to know how much memory (in bytes) a Map (or any variable) is occupying ?

Phillipp

Phillipp

You have no reason to believe the network can fail?

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

Touché !
That is a very good point we are overlooking. Although, IIRC, gun requests also time out. But am I not sure how the workers behave upon timeouts.

Nice catch !

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews