Managing your VPS memory usage

 

Overview

This article explains how your VPS uses memory and different options to monitor and troubleshoot memory issues when they arise.

Prerequisites

To run the commands in this article via SSH, you must have a Shell user configured in your panel and general knowledge of the UNIX Shell.

Baseline memory usage

Every DreamHost VPS uses roughly 100 MB of memory as a baseline without any user processes running. That's because several system processes must run for your VPS to work (e.g, sshd, proftpd). This must be considered when allocating memory for your VPS since that initial 100 MB is largely unusable for your site processes.

Viewing memory usage in the panel

To view your VPS memory usage graph, open the VPS page and click the Manage button to the right of your server. This graph is updated daily to display an average of the day's use, so it helps you see usage trends.

Auto-Boost

DreamHost provides a service called Auto-Boost that temporarily protects your server during memory spikes to ensure your website keeps running smoothly. The graph displays any instances when Auto-Boost has been activated.

The top, free, and ps aux commands

The following commands should be run on a regular basis over time to gain more information to help you diagnose the particular issue you're seeing. To run the following commands, you must:

  • Log into your server via SSH
  • In your terminal application, open two connections in separate tabs to easily review the results.

The 'top' command

The top command displays the currently active processes, the % of the CPU they're using, how much memory, and which user is running it. In your terminal, type in top. Then press the Enter key.

[server]$ top

Sorting by memory

Once top is running, press Shift + m to sort processes by memory usage.

PID   USER      PR  NI   VIRT  RES  SHR   S  %CPU  %MEM    TIME+   COMMAND
14871 exampleu  20   0   250m  14m  8340  S   5     1.3   0:04.12  php82.cgi
23591 exampleu  20   0   250m  12m  8256  S   6     1.3   0:00.36  php82.cgi
23652 exampleu  20   0   250m  13m  8256  S   6     1.3   0:00.18  php82.cgi
23654 exampleu  20   0   250m  15m  8252  S   6     1.3   0:00.17  php82.cgi

The memory allocation you set in your panel relates to physical memory being used (as opposed to virtual memory). This corresponds to the RES column in the output. In this column, you can see how much memory each process is using. In this case, they all use 12–15 MB of memory.

The values you see may be in kilobytes. To change to MB, just press the letter e. Keep pressing to alternate between the different memory units.

Keep in mind that Apache and PHP processes multiply as your site receives a high amount of traffic and starts serving more requests. As a result, memory usage could grow exponentially during these times.

The 'free' command

In your other terminal window, run free -m to display the current memory usage in megabytes. 

[server]$ free -m
             total        used        free      shared  buff/cache   available
Mem:          1000         233         114          21         652         766
Swap:            0           0           0
  • total: In the above example, the total available memory is roughly 1GB. You can see this value as 1000MB under the total column.
  • used: Under the used column in the first row, it shows 233 MB used.
  • available: Under the available column, it shows 766 MB available. This is the total memory available for starting new applications or processes.

The total you see should correspond to the amount of memory you allocated to your VPS in your panel.

Visit linuxatemyram.com for more details about memory usage.

The 'ps aux' command

The ps command supplies you with similar information as top, but simply displays a snapshot of the active processes and their usage. This is useful if you only want to see particular processes.

Apache processes

If you only want to see Apache processes, run the following command:

[server]$ ps aux | grep apache

That will "pipe" (or pass) the output from ps aux to the grep command, which will filter that output by looking for the string apache in it. Any line with that output is displayed.

PHP processes

Run the following command to display PHP processes that are currently running:

[server]$ ps aux | grep php

Recovering from memory saturation

Memory saturation is when your VPS is using up its entire allotment of memory. When this happens, a variety of things will start to happen.

  • Your sites will likely stop responding in many cases
  • Other essential processes on your VPS will stop responding as well, such as the SSH server, FTP server, streaming media server, and so.
  • Your VPS can enter a state where you are unable to even log in due to memory saturation.

Checking for the cause

You can usually tell if this is happening by checking a few things:

Checking your resource graph in the panel

If the resource usage graph shows your memory spiking high above the allocated memory, then this is likely what's happening.

Run the 'free' command

If you can still log into your VPS via SSH, try running the free -m command to see how much memory you have available. The closer that is to zero, the worse you are. At zero, you've reached total memory saturation, and what happens next can be unpredictable.

Fixing the issue

The steps in this article explain how to locate the source of memory issues. From there, you'll have to continue troubleshooting what you've found to determine the original cause. 

For example, if PHP processes are causing memory issues, and your website is built on WordPress, you could start troubleshooting common WordPress topics.

If, however, your website is built on custom code, the output of the commands above could point you to the specific script causing the memory issues.

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?