Managing your VPS memory usage


Every DreamHost virtual private server (VPS) uses roughly 100 MB of memory as a baseline without any user processes running. That's because there are a number of system processes that have to run in order for your PS to work (sshd, proftpd, and so on). It's important that you keep this in mind when allocating memory for your PS since that initial 100 MB of memory is largely unusable for your site processes.

After that initial 100 MB, most of the memory that is used by your PS consists of Apache and PHP processes (or in the case of a Rails applications, Passenger and Rails processes).

Example of how processes use memory

Whenever someone views your website, the PHP file for the page they view is parsed and executed by a PHP CGI process. At the same time, an Apache process will need to run, in order 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. In fact, Apache and PHP will continue to spawn processes as requested until your PS is completely out of memory.

You will need to be careful how you set things up to prevent such problems. This often leads to problems where websites on a PS will stop working due to memory saturation during peak hours and will then start working normally again once the traffic dies down to more manageable levels.

Raising the amount of Apache processes allowed to spawn

As of March 2016, new VPS plans are created with a specific amount of disk space and RAM. It's not possible to adjust the RAM on these new machines. However, older VPS machines still have the ability to adjust their RAM allocation.

Another important consideration is that all private servers are configured to scale the number of Apache instances allowed to spawn with the amount of memory you allocate for it. In most cases, this works fine when your site isn't getting a lot of traffic. If your site is getting a significant amount of traffic, increasing the memory allocation basically allows more of that significant traffic to be served, where previously it would have been getting extremely slow load times or errors. So, until you hit the amount of memory needed to serve all of your viewers optimally, the memory usage for your site may scale upwards as you increase your memory limit.

Checking your memory usage

You can view a graph of your private server’s memory usage in your panel.

  1. Go to the VPS page.
  2. To the right of your VPS, click the RAM graph icon under the ‘Usage’ column.
    01 VPS Troubleshooting.fw.png

The memory usage graph on your web panel is helpful for seeing usage trends, but isn't really helpful for getting a good picture of what's actually going on in real time since these graphs are generally updated once a day around midnight PDT/PST and are an average of the day's use.

The top, free, and ps aux commands

To obtain more information on exactly what's going on, you'll need to SSH into your PS.

Open two connections in separate windows so you can easily review the results.

Once you're in, the primary tools you'll use are top -c, free -m, and ps aux.

Using the top command

The top command displays the current active processes, the % of the CPU they're using, how much memory, which user is running it, and so on. 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, it's important to know what information you need to look at. The memory allocation you set in your panel relates to physical memory being used (as opposed to virtual memory). That memory corresponds to the RES column in top output. Below is what top output sorted by memory ('[Shift] + m') might look like (the actual processes will likely be different on your PS):

14871 exampleu  20   0  250m  14m 8340  S  5     1.3   0:04.12  php71.cgi
23591 exampleu  20   0  250m  12m 8256  S  6     1.3   0:00.36  php71.cgi
23618 exampleu  20   0  250m  13m 8256  S  4     1.3   0:00.19  php71.cgi
23684 exampleu  20   0  250m  12m 8256  S  7     1.3   0:00.10  php71.cgi
23652 exampleu  20   0  250m  13m 8256  S  6     1.3   0:00.18  php71.cgi
23654 exampleu  20   0  250m  15m 8252  S  6     1.3   0:00.17  php71.cgi

In the above example, you'll see a series of php71.cgi processes running which means the site spawning these processes is running under PHP 7.1. In the RES column, you can see how much memory each is using. In this case, they're all using between 12–15 MB of memory. On a busy PS, you'll likely have quite a few 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 begin to receive a lot of traffic.

Using the free command

In your other window, you can run free -m. This command displays the current memory usage on your PS and should look something like this:

                    total        used        free     shared    buffers     cached
 Mem:               4049        3941        107            0        123       1639
 -/+ buffers/cache:             2178       1870
 Swap:              6165          42       6122

The -m option tells the command to display the memory usage in megabytes.

total — In the above example, the total available memory is roughly 4GB. You can see this value as 4049MB under the 'total' column.

used — Under the 'used' column in the second row it shows 2178 MB. This means 2.1GB is used.

free — Under the 'free' column in the second row it shows 1870MB. This means 1.8GB is free.

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

For more details about what memory usage looks like, visit the following:

Using the ps aux command

The ps command supplies you with similar information as top, but simply takes a snapshot of the active processes and their usage and displays it on your screen. 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 to the grep command which will filter that output on a per line basis looking for the string "apache" in it. Any line with that output is displayed.

Another useful variation is:

[server]$ ps aux | grep php

This allows you to observe all the PHP processes that are running.

See also

Did this article answer your questions?

Article last updated PST.