# Out of memory on Elixir/Phoenix app when connecting \`iex\` session

**URL:** <https://community.fly.io/t/out-of-memory-on-elixir-phoenix-app-when-connecting-iex-session/3852>\
**Category:** Phoenix\
**Tags:** elixir\
**Created:** [January 28, 2022, 12:01pm UTC](https://community.fly.io/t/out-of-memory-on-elixir-phoenix-app-when-connecting-iex-session/3852 "2022-01-28T12:01:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jordelver](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jordelver/32/2220_2.png) [@jordelver](https://community.fly.io/u/jordelver)\
**Post date:** [January 28, 2022, 12:01pm UTC](https://community.fly.io/t/out-of-memory-on-elixir-phoenix-app-when-connecting-iex-session/3852/1 "2022-01-28T12:01:28Z")

</div>

I’m trying out Fly for the first time to deploy an Elixir Phoenix app with Postgres.

I’ve created two apps with 256MB of memory 1) for the app, and 2) for Postgres.

I got the app deployed and seemingly working, so tried connecting to `iex` using `fly ssh console` followed by `/app/bin/<appname> remote` and it connected fine. Then when I try to create a record in the database it terminates due to being out of memory.

```auto
***ERROR: Shell process terminated! (^G to start new job)***

```

And the logs from `fly logs` show the app restarting.

```auto
2022-01-28T11:56:58.234 app[3b89ab36] lhr [info][48902.313199] Out of memory: Killed process 510 (beam.smp) total-vm:1857876kB, anon-rss:190028kB, file-rss:0kB, shmem-rss:0kB, UID:65534 pgtables:580kB oom_score_adj:0
2022-01-28T11:56:58.351 app[3b89ab36] lhr [info]Main child exited with signal (with signal 'SIGKILL', core dumped? false)
2022-01-28T11:56:58.353 app[3b89ab36] lhr [info]Reaped child process with pid: 551, exit code: 0
2022-01-28T11:56:58.354 app[3b89ab36] lhr [info]Reaped child process with pid: 568, exit code: 0
2022-01-28T11:56:58.354 app[3b89ab36] lhr [info]Starting clean up.
2022-01-28T11:56:58.360 app[3b89ab36] lhr [info]Process appears to have been OOM killed!
2022-01-28T11:57:05.074 runner[3b89ab36] lhr [info]Starting instance
2022-01-28T11:57:05.117 runner[3b89ab36] lhr [info]Configuring virtual machine
2022-01-28T11:57:05.118 runner[3b89ab36] lhr [info]Pulling container image
2022-01-28T11:57:05.638 runner[3b89ab36] lhr [info]Unpacking image
2022-01-28T11:57:05.643 runner[3b89ab36] lhr [info]Preparing kernel init
2022-01-28T11:57:06.118 runner[3b89ab36] lhr [info]Configuring firecracker
2022-01-28T11:57:06.338 runner[3b89ab36] lhr [info]Starting virtual machine
2022-01-28T11:57:06.488 app[3b89ab36] lhr [info]Starting init (commit: 0c50bff)...
2022-01-28T11:57:06.513 app[3b89ab36] lhr [info]Preparing to run: `/app/bin/server` as nobody
2022-01-28T11:57:06.540 app[3b89ab36] lhr [info]2022/01/28 11:57:06 listening on [fdaa:0:36fc:a7b:2809:3b89:ab36:2]:22 (DNS: [fdaa::3]:53)

```

But the metric graph for the app doesn’t seem to indicate being out of memory?

 ![CleanShot 2022-01-28 at 11.53.12@2x](https://global.discourse-cdn.com/fly1/original/3X/3/6/361e03fbb2ffd67ecb03278a9ce99b8531bffa01.png)

Scaling the memory to 512MB let me create a record in the database using `iex`.

I was expecting that 256MB would be enough for what is, at the moment, basically a skeleton Phoenix app, but perhaps my expectations are incorrect?

Thanks for any insight!

---

<div class="post-metadata">

**Author:** ![chrismccord](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/chrismccord/32/50976_2.png) [@chrismccord](https://community.fly.io/u/chrismccord)\
**Post date:** [January 28, 2022, 2:34pm UTC](https://community.fly.io/t/out-of-memory-on-elixir-phoenix-app-when-connecting-iex-session/3852/2 "2022-01-28T14:34:09Z")

</div>

I’ve remote iex’d into a 256mb instance countless times without OOM, so it’s hard to say what is going on here without seeing your app. Is “basically skeleton” really only some crud `Repo.insert`'s or are you doing other more specialized things? My current non-trivial app runs about 180mb under general operation so your baseline looks reasonable. Note that the erlang VM running a stock phoenix app out of the box runs like 16mb, but BEAM will use whatever memory it needs under operation.

---

<div class="post-metadata">

**Author:** ![jordelver](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jordelver/32/2220_2.png) [@jordelver](https://community.fly.io/u/jordelver)\
**Post date:** [January 28, 2022, 3:07pm UTC](https://community.fly.io/t/out-of-memory-on-elixir-phoenix-app-when-connecting-iex-session/3852/3 "2022-01-28T15:07:10Z")

</div>

Thanks for the response.

> [@chrismccord](#):
>
> Is “basically skeleton” really only some crud `Repo.insert` 's or are you doing other more specialized things?

It’s pretty uninteresting at the moment 🙂 I generated the app under Phoenix 1.5.8 and recently upgraded to 1.6.6. It has `phx.gen.auth` authentication added, and it has a single JSON API endpoint. Nothing much else going on at the moment. [You can find it here](https://github.com/jordelver/pilfer).

The actual code I was running in `iex` was the user registration function that `phx.gen.auth` generates for you:

```auto
def register_user(attrs) do
  %User{}
  |> User.registration_changeset(attrs)
  |> Repo.insert()
end

```

I noticed that these lines are logged as soon as I run `/app/bin/<name> remote`. Seems like something is happening with a child process straight away?

```auto
2022-01-28T14:53:31.166 app[3b89ab36] lhr [info]Reaped child process with pid: 611, exit code: 0
2022-01-28T14:53:31.167 app[3b89ab36] lhr [info]Reaped child process with pid: 613, exit code: 0
2022-01-28T14:53:31.169 app[3b89ab36] lhr [info]Reaped child process with pid: 632 and signal: SIGUSR1, core dumped? false

```

I also played around some more in `iex` - created some other records etc and it worked fine. But if I call the `register_user` function it kills `iex` every time.

> [@chrismccord](#):
>
> My current non-trivial app runs about 180mb under general operation so your baseline looks reasonable. Note that the erlang VM running a stock phoenix app out of the box runs like 16mb, but BEAM will use whatever memory it needs under operation.

Interesting. Yes, seems like this app should be fine.

🤔

---

<div class="post-metadata">

**Author:** ![jordelver](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jordelver/32/2220_2.png) [@jordelver](https://community.fly.io/u/jordelver)\
**Post date:** [January 29, 2022, 1:38pm UTC](https://community.fly.io/t/out-of-memory-on-elixir-phoenix-app-when-connecting-iex-session/3852/4 "2022-01-29T13:38:37Z")

</div>

My original report above said that this issue happened when registering a user. I since noticed that it also happened when logging in via the web. I had inkling that maybe Argon2 had something to do it because password hashing is involved in both of those actions.

I then [found this on the Elixir Forum](https://elixirforum.com/t/staging-environment-how-to-debug-out-of-memory-errors-in-production-on-fly-io/42763/7) which describes the issue and possible fixes.

I changed `config/prod.exs` to have the following as described [here](https://elixirforum.com/t/staging-environment-how-to-debug-out-of-memory-errors-in-production-on-fly-io/42763/7?u=jordelver).

```auto
# Set cost of password hashing complexity to use less memory
config :argon2_elixir, t_cost: 2, m_cost: 15 # ~60ms

```

And now everything seems good. Will report back if anything changes.

Thanks!

---

<div class="post-metadata">

**Author:** ![chrismccord](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/chrismccord/32/50976_2.png) [@chrismccord](https://community.fly.io/u/chrismccord)\
**Post date:** [January 29, 2022, 2:12pm UTC](https://community.fly.io/t/out-of-memory-on-elixir-phoenix-app-when-connecting-iex-session/3852/5 "2022-01-29T14:12:01Z")

</div>

Ah good catch! We actually made a change to `phx.gen.auth` in phoenix 1.6.1 for exactly this reason. (using bcrypt instead):

```auto
## 1.6.1 (2021-10-08)

### Enhancements
  ...
  * [phx.gen.auth] No longer set `argon2` as the default hash algorithm
   for `phx.gen.auth` in favor of bcrypt for performance reasons on smaller
   hardware

```

---

<div class="post-metadata">

**Author:** ![jordelver](https://sea1.discourse-cdn.com/fly1/user_avatar/community.fly.io/jordelver/32/2220_2.png) [@jordelver](https://community.fly.io/u/jordelver)\
**Post date:** [January 29, 2022, 5:12pm UTC](https://community.fly.io/t/out-of-memory-on-elixir-phoenix-app-when-connecting-iex-session/3852/6 "2022-01-29T17:12:37Z")

</div>

Thanks for letting me know. I may consider going back to bcrypt. Glad it’s fixed now 🙂
