From f48adc0f0523cf1f170de2e174bfab84f37924dc Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sun, 18 Jan 2026 15:09:22 -0600 Subject: [PATCH] Add Clean Up Memory-Hungry Rails Console Processes as a Rails TIL --- README.md | 3 +- ...p-memory-hungry-rails-console-processes.md | 43 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 rails/clean-up-memory-hungry-rails-console-processes.md diff --git a/README.md b/README.md index 50fa8d1..3512d92 100644 --- a/README.md +++ b/README.md @@ -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). -_1729 TILs and counting..._ +_1730 TILs and counting..._ 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 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) +- [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) - [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) diff --git a/rails/clean-up-memory-hungry-rails-console-processes.md b/rails/clean-up-memory-hungry-rails-console-processes.md new file mode 100644 index 0000000..0dce3f8 --- /dev/null +++ b/rails/clean-up-memory-hungry-rails-console-processes.md @@ -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.