Fl4m3Ph03n1x
Processes memory spike until crash - any ideas what's causing it?
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










First 10 of 22 Posts
idi527
What have you actually tried to find the problematic processes? Have you tried listing all processes, checking their memory?
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
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
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
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
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.dumpfile sitting at the top of my release directory, right next to thebindir. 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
And what do they do?
Fl4m3Ph03n1x
These workers hold a State with a connection pid (which is in reality the PID from a
gunprocess) and a map with streams (when making requests with gun, each request is a Stream )Following is the state of each worker:
Now, what could possibly go wrong?
The only thing that goes through my mind is that the
active_streamsmap may grow forever if a stream never receives the:fin_packetnecessary 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
You have no reason to believe the network can fail?
Fl4m3Ph03n1x
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 !