Sales:  800-573-7846 or Live Chat

The ServInt Source

Understanding and Troubleshooting RAM in Linux

tech bench 2RAM (Random Access Memory) is computer storage that holds the data of running processes (i.e. programs). That data includes the image of the program itself, the data that the program uses to interact with the computer’s operating system, and the data that the program is actually manipulating to do whatever useful thing the program is written to do. Consequently, RAM needs to be a very fast data store that gives a running program nearly instant access to the data it needs to operate. It is also volatile — it does not survive reboot. By contrast, data on disk (whether traditional hard disk, or otherwise) is designed for long-term storage and large capacity at the expense of speed.

How much RAM is my server using?

35727018_1a624b0bd0_o

A server can contain 1 to 16+ sticks of RAM, which each typically hold 1-4Gb of storage.

The easiest way to see memory usage is to run the free command in SSH (use the -m tag to view the results in megabytes). Below is an example.

free -m
                   total  used  free shared buffers cached
Mem:               11796  2302  9494      0     224   1199
-/+ buffers/cache:         877 10919
Swap:               8189     0  8189

The line that most accurately describes the amount of memory being used and how much is available for future usage is -/+ buffers/cache. This line subtracts buffers and caches from the used column of memory and adds buffers and caches to the free column in order to show the user exactly what is currently being used by system processes and what is available in reserve.

Caches are memory that stores recently accessed data in case it needs to be accessed again. (Virtuozzo-based VPS technology does not use RAM buffers.) The main difference between cached RAM and used RAM is that caches can and will be released and reused when there is a more immediate and direct need for this memory.

If you use cPanel, you can also access the information in free under “Service Status.”

Have you heard? ServInt has doubled the guaranteed RAM for its VPS line.

Why does my server use so much RAM, and how can I reduce my RAM usage?

The first step in troubleshooting high RAM usage is to narrow down which process[es] is using the majority of the RAM. Running a top command in the shell is a good place to start. Once in top, type [shift] + m to sort the list of processes by memory. Let the output refresh for the next 30-60 seconds and watch to see which processes are listed at the top. The RES column is the closest to actual RAM usage, the %MEM column is how the top is organized.

  PID USER   PR NI  VIRT  RES  SHR S %CPU %MEM     TIME+  COMMAND
31307 named  20  0 2492m 108m 2424 S  0.0  0.7   24:49.34 named
28178 mysql  20  0 12.7g  88m 3864 S  2.6  0.5 2695:47    mysqld
19851 nobody 20  0  301m  80m 5976 S  0.0  0.5    0:13.13 httpd
19797 nobody 20  0  298m  77m 6208 S  0.0  0.5    0:13.43 httpd
20097 nobody 20  0  297m  76m 5788 S  0.0  0.5    0:07.97 httpd
30932 nobody 20  0  295m  74m 6344 S  0.0  0.5    0:02.63 httpd
30920 nobody 20  0  295m  74m 4448 S  0.0  0.5    0:01.45 httpd

Note that some processes fork (create child processes). The child processes will show up individually in top. In the above top, it looks as if named (nameserver) is using up the most memory, but in actuality it’s all of the httpd (apache) processes together that use up the majority of the memory. Running pstree will show you which processes are running, what is spawning them, and how many there are.

pstree
init─┬─/usr/local/cpan───spamd\040child
     ├─atd
     ├─cPhulkd\040-\040proce
     ├─cpanellogd\040-\040sl
     ├─cpdavd\040-\040accept
     ├─cpsrvd-ssl
     ├─crond
     ├─dbus-daemon
     ├─dovecot─┬─dovecot-auth
     │         ├─2*[imap-login]
     │         └─2*[pop3-login]
     ├─exim
     ├─httpd─┬─89*[httpd]
     │       └─leechprotect
     ├─kthreadd/18836───khelper/18836
     ├─mysqld_safe───mysqld───8*[{mysqld}]
     ├─named───34*[{named}]
     ├─pure-authd
     ├─pure-ftpd───2*[pure-ftpd───pure-ftpd]
     ├─queueprocd\040-\040wa
     ├─rsyslogd───3*[{rsyslogd}]
     ├─sshd───sshd───sshd───sftp-server
     ├─tailwatchd
     ├─udevd
     ├─vzctl───bash───pstree
     └─xinetd

Depending on which process is using up the majority of your RAM, the solution (assuming there is one) will vary. The next step will be to Google “reduce ____ memory usage” for which ever process is eating up RAM.

If you ever get stuck or are not sure how to proceed, contact the MST through the ServInt customer Portal and we can give you more information and suggestions on how to proceed.

Photo by superpuppy.

About Dominik Poetek

Dominik Poetek is the Head of Escalation Support at ServInt. He has been a part of ServInt's Managed Services Team since April of 2004.


The ServInt Source | Web Hosting Blog