This article explains how your VPS uses memory and different options to monitor and troubleshoot memory issues when they arise.
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 (sshd, proftpd, and so on). This must be considered when allocating memory for your VPS since that initial 100 MB is largely unusable for your site processes.
After that initial 100 MB, most of the memory used by your VPS consists of Apache and PHP processes (or, in the case of Rails applications, Passenger and Rails processes).
Example of how PHP processes use memory
When your website is visited, the PHP file for the page viewed is parsed and executed by a PHP process. At the same time, an Apache process runs to serve the resulting HTML output from the script to the browser.
The number of these processes that run is proportional to the amount of traffic you have. For example, if you have a large influx of traffic, additional PHP and Apache processes will spawn to facilitate that traffic. Apache and PHP will continue to spawn processes as requested until your VPS is completely out of memory.
This often leads to problems where websites on a VPS will stop working due to memory saturation during peak hours and will start working normally again once the traffic dies down to more manageable levels.
For this reason, you must be careful how you set things up to prevent such problems.
Viewing memory usage in the panel
You can view your VPS memory usage graph in your panel.
- Go to the VPS page.
- To the right of your VPS, click the RAM graph icon under the Usage column.
This graph is generally updated once a day, around midnight PDT/PST to display an average of the day's use.
Since it is updated once per day, it helps you see usage trends. However, it is not useful to get a good picture of what's happening in real-time. The commands below help you to view real-time data.
The top, free, and ps aux commands
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. You can then run the following commands to troubleshoot memory issues:
- free -m
- ps aux
The 'top' command
In your terminal, type in top. Then press the Enter key.
The top command displays the currently active processes, the % of the CPU they're using, how much memory, which user is running it, and so on.
Sorting by memory
Once top is running, you can press Shift + m to sort processes by memory usage rather than by CPU usage.
While examining your top output, knowing what information you need to look at is important. 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.
Below is what top output sorted by memory (Shift + m) might look like (the actual processes will be different on your VPS):
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.
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 23618 exampleu 20 0 250m 13m 8256 S 4 1.3 0:00.19 php82.cgi 23684 exampleu 20 0 250m 12m 8256 S 7 1.3 0:00.10 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
In the above example, you'll see a series of php82.cgi processes running which means the site spawning these processes is running under PHP 8.2. In the RES column, you can see how much memory each is using. In this case, they all use 12–15 MB of memory. On a busy VPS, you'll likely have several Apache processes running. Those average around 9-11 MB of memory each. Apache and PHP processes multiply as your sites start serving more requests, so you can see that memory usage can quite easily skyrocket if you receive a lot of traffic.
The 'free' command
In your other window, run free -m to display the current memory usage in megabytes.
[server]$ free -m
The output will look something like this:
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 the following link 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 a particular process(es).
For instance, if you only want to see running 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, looking for the string apache in it. Any line with that output is displayed.
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.
The commands above should be run on a regular basis over time to gain more information to help you diagnose the particular issue you're seeing.