mirror of
https://github.com/jbranchaud/til
synced 2026-03-04 15:08:45 +00:00
Compare commits
2 Commits
bd58be8fda
...
f48adc0f05
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f48adc0f05 | ||
|
|
9773f10b84 |
@@ -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).
|
||||
|
||||
_1728 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)
|
||||
@@ -1773,6 +1774,7 @@ If you've learned something here, support my efforts writing daily TILs by
|
||||
- [Unrestrict Where ripgrep Searches](unix/unrestrict-where-ripgrep-searches.md)
|
||||
- [Update Package Versions Known By asdf Plugin](unix/update-package-versions-known-by-asdf-plugin.md)
|
||||
- [Use fzf To Change Directories](unix/use-fzf-to-change-directories.md)
|
||||
- [Use Negative Lookbehind Matching With ripgrep](unix/use-negative-lookbehind-matching-with-ripgrep.md)
|
||||
- [Use Regex Pattern Matching With Grep](unix/use-regex-pattern-matching-with-grep.md)
|
||||
- [View A Web Page In The Terminal](unix/view-a-web-page-in-the-terminal.md)
|
||||
- [View The Source For A Brew Formula](unix/view-the-source-for-a-brew-formula.md)
|
||||
|
||||
43
rails/clean-up-memory-hungry-rails-console-processes.md
Normal file
43
rails/clean-up-memory-hungry-rails-console-processes.md
Normal 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.
|
||||
42
unix/use-negative-lookbehind-matching-with-ripgrep.md
Normal file
42
unix/use-negative-lookbehind-matching-with-ripgrep.md
Normal file
@@ -0,0 +1,42 @@
|
||||
# Use Negative Lookbehind Matching With ripgrep
|
||||
|
||||
The most straightforward way to use `ripgrep` is to hand it a pattern. It will
|
||||
take that pattern and move forward through each file trying to find matches.
|
||||
|
||||
```bash
|
||||
$ rg 'TwilioClient\.new'
|
||||
```
|
||||
|
||||
That will find all occurrences of `TwilioClient.new` in available project files.
|
||||
|
||||
What if that pattern is too permissive though? That is going to match on
|
||||
occurrences of `TwilioClient.new` as well as things like
|
||||
`LoggingTwilioClient.new`. If we want to exclude the latter, there are a few
|
||||
ways to do that. One of them being the use of [the _negative lookbehind_ regex
|
||||
feature](https://www.pcre.org/current/doc/html/pcre2syntax.html#SEC23) that is
|
||||
available with PCRE2 (Perl-Compatible Regular Expressions).
|
||||
|
||||
A _negative lookbehind_ is like a standard pattern. We look forward through the
|
||||
document for the base pattern (like `TwilioClient\.new`). However, once we find
|
||||
that match, we then look back at the previous characters and if they match our
|
||||
negative lookbehind pattern, then it is no longer a positive match.
|
||||
|
||||
We can use one of the following to forms to achieve this:
|
||||
|
||||
```
|
||||
(?<!...) )
|
||||
(*nlb:...) ) negative lookbehind
|
||||
(*negative_lookbehind:...) )
|
||||
```
|
||||
|
||||
For instance, here is what this looks like for our example:
|
||||
|
||||
```bash
|
||||
$ rg -P '(?<!Logging)TwilioClient\.new'
|
||||
```
|
||||
|
||||
Note: we have to use the `-P` flag to tell `ripgrep` that we are using PCRE2
|
||||
syntax. Otherwise, it assumes a simpler regex syntax that doesn't support
|
||||
_negative lookbehind_.
|
||||
|
||||
See `man rg` for more details.
|
||||
Reference in New Issue
Block a user