2017.05.21 05:00 | Kurt Tomlinson
Are you running a Ruby on Rails web app? If you are, you're probably going to run into memory problems sooner or later. In that case, you're going to want to know how to pinpoint your memory-hogging processes.
The instructions below assume you're running your production server on Ubuntu. To learn more about Ubuntu, check out Ubuntu Unleashed 2017 Edition (Includes Content Update Program): Covering 16.10, 17.04, 17.10 (12th Edition). Either way, scroll down to keep reading.
The best way to figure out which processes are using the most memory on your server is with top. Top is an endlessly configurable process viewer. To monitor the memory usage on my server I configure top to sort processes by memory used, display a gauge showing consumed memory, and show the process names of each process.
To sort processes by memory used, run top, press f, use the arrow keys to highlight MEM, press s to set the sort order to memory, and press q to return to the main screen in top.
When in the main screen in top, press m to toggle through the memory gauge displays. I prefer the display with the filled-in memory gauge. It gives me a really easy way to see how much memory my server is using and how close it is to having to start using swap.
Top has two different ways to display process names. To toggle between them, press c. The default way top displays process names has the annoying habit of displaying all Ruby processes as "ruby" regardless of if it's a rails app process, a rails runner process, or a delayed job process.
The c command lets you tell top that you want to see the command used to start a process instead of the program's name. After all, seeing a process labeled rails runner "ConfirmationMailer.run" is much more helpful than if it was labeled just ruby.
rails runner "ConfirmationMailer.run"
Finally, once you've got top set up the way you like, press W on the main screen of top to save your top configuration. This configuration will be automatically loaded the next time you run top. (Notice, this command is a capital "W", not a lowercase "w".)
The reason I'm sharing this with you is because these are the steps I took to identify some processes that were unnecessarily using hundreds of MB of RAM on my production server. What were those processes you ask? Well, one was Spring, an application preloader. You can read about how to remove Spring from your production server if you notice that it somehow got added there.
The other process I had unnecessarily running on my production web server was MySQL. I run a MySQL server on my development server, and I forgot to remove it from my deploy script when I updated it for production deploys. Uninstalling mysqld from my production server freed up between 100 and 200 MB of RAM with very little effort.
Have you discovered any unnecessary processes running on your production server? Let me know in the comments!
Photo by Steve Parker