Whenever someone complains about a slow computer, the first thing I check is how many Chrome tabs they have open. Chrome hogs memory like no other. For users, this means Chrome is snappy and responsive, but oftentimes it comes at the expense of crowding out other programs. To get an idea of how much memory Chrome is really using, I wrote a quick bash oneliner.
Why does Chrome Hog Memory?
An article posted to Hacker News recently brought some light to the question of why Chrome and Firefox suck up so much memory: Adblock Plus. The general idea is that the excessive use of iframes in most websites today ramps up the amount of processing that Adblock Plus has to do, driving memory usage through the roof. For more specifics, check out the rest of of the article.
The Oneliner
For the impatient, here’s the code. It uses standard Unix tools:
Pretty isn’t it? If you want to save this as an alias for handy use, add this line to your ~/.bashrc (or appropriate configuration file):
It outputs a percentage. Here’s the alias in action:
Explanation
There’s a lot of good stuff going on here, so let’s take it step-by-step.
First, we’ll need a program that tells us memory usage. I’m sure
there are many, but I’m familiar with ps
. After checking
out the man page for a few options, I came up with ps -ev
,
to show all information about all processes. Maybe wasteful, but it
works.
Notice that there’s a convenient column describing memory usage as a
percentage of total available memory, as well as what command is being
run in that process. Let’s make sure that we’re looking at only the
processes running some sort of Chrome service before totaling up the
memory. We can find these lines with
ps -ev | grep -i chrome
(the -i means case-insensitive).
Due to the way I clipped the previous sample output, nothing changes in
the first five lines, but rest assured: we’re only looking at Chrome
processes now.
Now it’s time to get rid of all the other nonsense that we included
with ps -ev
. Luckily, there’s a handy tool called
awk
that makes parsing text by column easy. If we want to
print the 12th column (which just so happens to contain the memory
consumption!) we can do awk '{print $12}'
:
Finally, I found myself needing a way to add up a column of numbers.
A quick Google search led me to this
StackOverflow question, and I picked the awk
solution
because I knew I could just pipe the input to awk (as opposed to having
to do weird hacks to get it to work with a bash for loop):
Of course, you could change the last awk
command to
print out something fancier like
There you have it! Bash oneliners save the day yet again.
Update
After writing this article, I stopped using Ad Block Plus, and I noticed a significant drop in Chrome’s memory usage. Obviously, though, that came at the cost of not blocking ads! Also, from time to time I would encounter a site that seemed sluggish, presumably because of all the ads attempting to be loaded. My simple solution to this was just to disable JavaScript on that page (I use an extension called Quick JavaScript Switcher), but this wasn’t an automated solution.
Then I discovered μBlock, an “efficient blocker for Chromium and Firefox.” The fancy graphs on it’s homepage convinced me to give it a shot, and from what I can tell so far it’s responsive and effective.