1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-18 22:48:02 +00:00

Add Clean Up Memory-Hungry Rails Console Processes as a Rails TIL

This commit is contained in:
jbranchaud
2026-01-18 15:09:22 -06:00
parent 9773f10b84
commit f48adc0f05
2 changed files with 45 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter). For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
_1729 TILs and counting..._ _1730 TILs and counting..._
See some of the other learning resources I work on: See some of the other learning resources I work on:
@@ -1073,6 +1073,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Check If ActiveRecord Update Fails](rails/check-if-activerecord-update-fails.md) - [Check If ActiveRecord Update Fails](rails/check-if-activerecord-update-fails.md)
- [Check If Any Records Have A Null Value](rails/check-if-any-records-have-a-null-value.md) - [Check If Any Records Have A Null Value](rails/check-if-any-records-have-a-null-value.md)
- [Check Specific Attributes On ActiveRecord Array](rails/check-specific-attributes-on-activerecord-array.md) - [Check Specific Attributes On ActiveRecord Array](rails/check-specific-attributes-on-activerecord-array.md)
- [Clean Up Memory Hungry Rails Console Processes](rails/clean-up-memory-hungry-rails-console-processes.md)
- [Code Statistics For An Application](rails/code-statistics-for-an-application.md) - [Code Statistics For An Application](rails/code-statistics-for-an-application.md)
- [Columns With Default Values Are Nil On Create](rails/columns-with-default-values-are-nil-on-create.md) - [Columns With Default Values Are Nil On Create](rails/columns-with-default-values-are-nil-on-create.md)
- [Comparing DateTimes Down To Second Precision](rails/comparing-datetimes-down-to-second-precision.md) - [Comparing DateTimes Down To Second Precision](rails/comparing-datetimes-down-to-second-precision.md)

View File

@@ -0,0 +1,43 @@
# Clean Up Memory Hungry Rails Console Processes
I noticed (using `htop`) that a remote server hosting a Rails app had most of
its RAM being actively consumed. This was hindering my ability to run a fresh
deploy because the deploy processes had to do a ton of memory swapping which
drastically slowed the whole thing down.
With some investigation, I discovered that most of the memory was being consumed
by a handful of `rails console` processes. I didn't have any known active `rails console` processes that I was using. That combined with the dates of these
processes starting way in the past suggested to me that these were abandoned
processes that hadn't been properly cleaned up.
```bash
server:~# ps aux | grep rails
32767 878915 0.0 0.0 1227160 936 pts/0 Ssl+ 2025 0:03 /exec rails console
32767 878942 0.9 6.5 830996 261748 pts/0 Rl+ 2025 249:51 ruby /app/bin/rails console
32767 3004097 0.0 0.0 1227160 692 pts/0 Ssl+ 2025 0:04 /exec rails console
32767 3004129 0.9 6.4 834672 257228 pts/0 Dl+ 2025 406:31 ruby /app/bin/rails console
32767 3048582 0.0 0.0 1227160 940 pts/0 Ssl+ Jan09 0:00 /exec rails console
32767 3048611 1.1 6.3 829936 253484 pts/0 Dl+ Jan09 60:50 ruby /app/bin/rails console
32767 3060033 0.0 0.0 1227160 944 pts/0 Ssl+ 2025 0:04 /exec rails console
32767 3060063 0.9 6.5 838084 260812 pts/0 Rl+ 2025 405:37 ruby /app/bin/rails console
root 3699372 0.0 0.0 7008 1300 pts/0 S+ 15:51 0:00 grep --color=auto rails
server:~# ps aux | grep 'rails console' | awk '{sum+=$6} END {print sum/1024 " MB"}'
1014.64 MB
```
As we can see by tacking on this `awk` command, these processes are consuming
1GB of memory.
Each of these is a pair of processes. A parent process (`/exec rails console`)
that kicks off and supervises the memory-hungry child process (`ruby /app/bin/rails console`).
To free up this memory, I targeted each of the parent processes with a `kill`
command one by one. For example:
```bash
server:~# kill 878915
```
I suspect that I may have left the occasional terminal tab open with one of
these `rails console` processes running and the SSH connection was getting
killed without the `rails console` getting killed with it.