tj0

tj0

Code memory size different from dev to prod

I have been running 1.15 locally for a while and 1.14 in production. I just upgraded production to 1.15.7 and noticed that the “code” size is different between dev/prod. I’m not sure what the expected behaviour is nor do I know what “code” memory means. I’m naively assuming it’s the size of the beam file AST in memory? Is this supposed to be static based on the deploy size or something else entirely? Erlang manual says:

The total amount of memory currently allocated for Erlang code. This memory is part of the memory presented as system memory.

So I would have assumed prod < dev actually. Anyone have any idea why this is different between dev / prod? Screenshots below.

Locally, 1.15.7 and OTP 26 gives 30MB of code size.

But on production, we have 48MB of code in memory.

Edit:
Also, a sidenote, trying to search for what memory code netted me this awesome post by @hauleth :rofl:

Also, an interesting note about binary memory:

Marked As Solved

tj0

tj0

Thank you @LostKobrakai @josevalim .

Code path pruning
I had skipped a rather important word when reading about code path pruning.

Code memory usage
Turns out running mix release.init creates rel/env.sh.eex which has the RELEASE_MODE option right at the top . I migrated from distillery, so it wasn’t obvious what I was looking for. Anyone starting off with a fresh project would easily find this option.

rel/env.sh.eex

# # Set the release to load code on demand (interactive) instead of preloading (embedded).
# export RELEASE_MODE=interactive

Also, this may not affect startup time in OTP 26.

Final results
Using interactive mode after going thru all the routes has reduced the memory:

Production is running at 23MB right now, but I expect it will grow to at least 30MB. Embedded mode which preloads everything had code memory usage at 48MB. Super-helpful when running on lower memory nodes. A MB saved is a MB earned. :slight_smile:

Also Liked

LostKobrakai

LostKobrakai

Releases default to preloading all modules, mix defaults to loading modules on demand to increase startup performance in favor of slightly slower first time response when a new module is used. That‘s likely the difference you see between prod and dev.

josevalim

josevalim

Creator of Elixir

Search for RELEASE_MODE in the docs. :slight_smile: interactive mode was slower to boot but that’s not necessarily the case from Erlang/OTP 26.

LostKobrakai

LostKobrakai

If you want to lower memory footprint you likely want to cut out what you don’t need – not disable preloading. What you actually need will eventually be loaded, so your footprint wouldn’t actually get smaller.

LostKobrakai

LostKobrakai

There is no automatic prunning of modules or functions. The feature added in 1.15 is around code paths, which brings mix projects closer to how releases are built by excluding everything but explicit dependencies mostly to otp applications. Previously all of OTP would be available for loading on demand.

To get a smaller codebase you’d need to lessen the (number of) applications you depend on, as there’s also nothing manual for pruning code at the module or function level.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement