stefanluptak

stefanluptak

Why is this reduced Stream consuming so much memory?

Hi there!

When I am trying to build my Commanded aggregate from events, it crashes my Render.com instance saying Out of memory (used over 2Gi).

I am building the aggregate from 100_000 events. That’s a lot, but nothing enormous I hope.
Every event returns something between 150-200 when executing :erts_debug.size(event).
The final (built) aggregate returns around 1_400_000 when calling :erts_debug.size(aggregate). It starts almost from zero and each event increases its size a bit.

EventStore is backed by PostgreSQL and the event data is stored as jsonb.

On my MacBook with this configuration it takes around 20 seconds.

Operating System: macOS
CPU Information: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Number of Available Cores: 4
Available memory: 16 GB
Elixir 1.10.2
Erlang 22.2.8

That seems reasonable, considering the number of events. I am just surprised, that this could consume so much memory. I though that the garbage collector is somehow getting rid of the already consumed events and already discarded versions of aggregate. Isn’t that the case?

This is the part responsible for building the aggregate. I assume it’s correct, I link it here just in case.

I am really curious to know why it is so memory hungry and what exactly is causing this. Any tips how to profile this? Or is it event worth it if that memory consumption is just a fact I have to deal with?

EDIT: Benchee says it consumes around 13GB when benchmarking with parameter memory_time: 10.

Thanks a lot.

Most Liked

bottlenecked

bottlenecked

Hi there, this reminds me of what can happen when a routing process handles large amounts of messages with reference counted strings in them (>64B) but makes very few (if any) allocations on its own. A strategy to deal with such problems is to force garbage collection every n number of messages, so that the references are removed and the strings can be gc’ed.

So… what if you fetched the events in batches, and forced a gc on the process that does the aggregation before moving on with the next batch? I’m not sure how easy is this to do with commanded, but perhaps it’s worth a try.

stefanluptak

stefanluptak

OK, I did some benchmarking and created a PR which should drasticaly improve the consumption. There are more details in its description here
I am really curious why the big difference in the memory consumption.
Any ideas someone?

Where Next?

Popular in Questions Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement