nikiiv

nikiiv

I am trying to get a list of gotchas for my prototype of an OMS for fun.
Given that Elixir (probably more of a BEAM question of course) is immutable, how does this exactly play within one process.
Lets say I have a struct for Customer and Product. Each one takes approx 10KB of memory
Then we have a struct for Order, that contains both keys for Customer and Product. Lets say with those two properties/fields set to nil an Order would occupy 1KB.
Say we have 1000 Customers and 10000 Products
If we combine each Customer and each Product into an Order, so 1_000_000 orders what is the expectation of the memory used within one process
1M by 21K each or 1M with 1K each plust two references to the 2x1000x10K (customers and products weight before combined into orders).

Given immutability I expect that Order would have reference and only if we mutate a field in the nested structure, we get a new memory allocation.

I expect atoms will be re-used, but lets for the sake of the question assume that no atoms are used in any of the 3 structs.

Technically an OMS (Order Management System) uses a lot of props that are static during the business day and data (say accounts) is changed after order execution which is a multi-step process, so it is important to know what to expect, when some of the props related to orders are re-used among multiple orders.
I can go ETS but.. do I really have to?
Any good blog posts or reads on the topic?

Thanks in advance

Showing Posts 1 to 8

NobbZ

NobbZ

Struct keys are atoms, so your assumption doesn’t hold.

Maps smaller 30ish entries are (internally) implemented as a tuple with a list of keys and a list of values, and some tree for bigger ones. Both variants allow you to share parts of the unchanged data.

So if you have a struct of size X, and “change” only one keys value, then you will have to copy the structs keys and value “pointers” and set the one changed value pointer to point at the new value. Allowing you to reuse all the actual keys and unchanged values.

nikiiv

nikiiv OP

It’s not the keys, it is the value of those keys that I am assuming aren’t atoms and the assumption is for the sake of understanding where data will be shared until modified (IOW copy on write). Can’t change keys in a struct, just the value

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Why guess when you can measure!

Check out :erts_debug.size to get the size (I think in words) of a given term.

NobbZ

NobbZ

That will show (roughly) the same values for the original and the “mutated” structs, as it does not take into account that there is shared data.

nikiiv

nikiiv OP

    {:ok, file} = File.read("/Users/...../memory_playground/java_error_in_idea_44801.log")
    z = %{content: file}
    l = []
    l = for _cnt <- 1..1_000_000, do: [z |[l]]
    IO.puts(:erts_debug.size(l))

It takes 10 min and it is still computing the word size..
Is it possible that it counts something that has already been accounted for if it is traversing the structure

nikiiv

nikiiv OP

Reading a 4MB pdf (Adopting Elixir.pdf) and adding it in a map with 1 key and then adding that map 100 times to a list
The size of the file is 3.8MB, after that using :erlang.memory(:total) ended up eating 6MB of memory.. An overhead of 2MB for 100 copies
So I guess until a change is made, the values are passed by reference..

Still can use some good blog posts about the topic..

LostKobrakai

LostKobrakai

How much of your data is binary data >64 bytes? Such binaries are put on a shared heap and reference counted instead of having multiple copies around.

For everything else Memory Usage — Erlang System Documentation v29.0.2 and :erts_debug.size are your friend.

rvnash

rvnash

My first question is what you mean by “reference”. There are no references in Elixir. There are only the data themselves.

Secondly, when a data structure, like a map here is modified, the underlying BEAM does not make a complete copy of the whole structure, although it appears to at the Elixir level abstraction. It’s actually some pretty clever data structure manipulation that can maintain mostly a limited memory impact.

I’ve found some clues to how this all works here. But to be honest, at the programming abstraction, I don’t know how to predict how it will perform.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews