Memory is still one of the things that most determines how well your Unix servers will perform. Knowing what commands will tell you what you need to know and what their responses mean will help keep you alert to potential problems and give you something to compare against when and if your system starts to slow down.
One of the most basic memory commands available on Linux systems is the free command. Free will report memory usage in kilobytes if you don't tell it otherwise. The command below uses the -m option to convert this to megabytes -- a little easier a format for me to deal with and maybe for you as well.
$ free -m total used free shared buffers cached Mem: 2026 1524 502 0 556 584 -/+ buffers/cache: 383 1643 Swap: 4094 40 4054
This system has 2 GB of memory, of which about 75% is in use. It has twice that much swap, but is using almost none of it.
The sar command, deriving from Solaris and sometimes available on Linux systems as well, is a great tool for looking at memory usage over a long period of time. This is especially helpful if you want to see how your systems are performing all week or to compare one day with another. The sar command is one of the best tools for routine performance reporting. Frequently set up to collect data periodically throughout the week, it can also be used to tell you how performance measurements look right now and those measurements include memory. To get two samples of five seconds each of memory usage, you can use the sar -r 5 2 command as shown below.
$ sar -r 5 2 SunOS boson 5.10 Generic_127127-11 sun4u 07/20/2014 16:26:16 freemem freeswap 16:26:21 5591 733840 16:26:26 5591 733840 Average 5591 733840
The units reported by sar are pages (freemem) and disk blocks (freeswap). To understand what these numbers really mean, you need to determine what these values represent. Fortunately, there are some commands to help you figure this out.
We can use the Solaris pagesize command to display the page size -- normally 8192 bytes or 8 KB.
$ pagesize 8192
For disk blocks, we need to look at the sizing parameters for the particular file system. Here, we see that the block size is also set to 8192 bytes.
$ df -g / / (/dev/dsk/c1t0d0s0 ): 8192 block size 1024 frag size 30256454 total blocks 21252254 free blocks 20949690 available 1822912 total files 1657400 free files 8388632 filesys id ufs fstype 0x00000004 flag 255 filename length
Of course, you still need to marry these numbers together -- the block size from the df -g command and the pagesize. You can do this with the expr command.
$ expr 5591 \* 8192 45801472
Numbers like 45801472 have me counting off the digits. You might like this expr command better as it gives you the measurement in MB. This is, however, free memory, not overall memory.
$ expr 5591 \* 8192 / 1000000 45
Getting an idea how much overall memory is installed on the system regardless of whether or not it's in use is even easier. Use the prtconf command and grep on the word "Memory" and you'll see something like this though, but hopefully something in the range of GB, not MB.
$ prtconf | grep Memory Memory size: 512 Megabytes
To get some quick memory information on a Linux system, you can also use the meminfo command. Looking at the meminfo file, we can see how much memory is installed as well as how much is free.
$ cat /proc/meminfo | head -2 MemTotal: 2074932 kB MemFree: 506820 kB
To make your display a little more interesting and add measurements for swap, run a command like this:
$ egrep --color 'Mem|Cache|Swap' /proc/meminfo MemTotal: 2074932 kB MemFree: 507144 kB Cached: 593552 kB SwapCached: 25304 kB SwapTotal: 4192956 kB SwapFree: 4151476 kB
In this command, we're using egrep to pull out any lines that have one of the three strings -- mem, cache, or swap -- within them.
The vmstat command is one you'll find both on Linux and Solaris. As you can see from the display below, it has a lot to tell you, but the output is just a tad cryptic and may vary from one flavor of Unix to another. The vmstat command also tells you about a lot more than memory. Though you can see the swap and free memory numbers below, we also see some stats related to disk activity and CPU usage.
linux$ vmstat procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ r b swpd free buff cache si so bi bo in cs us sy id wa st 0 1 41480 514464 570160 598228 0 0 0 3 1 0 15 15 70 0 0
solaris$ vmstat kthr memory page disk faults cpu r b w swap free re mf pi po fr de sr s0 s3 s3 -- in sy cs us sy id 0 0 0 847576 74392 8 7 45 0 0 0 0 0 0 -0 0 420 45 125 0 1 98
One of my favorite commands for looking at performance is top, simply because it tells me so much about how a system is performing and in a format that is easy to digest.
$ top top - 19:34:52 up 183 days, 7:58, 1 user, load average: 1.05, 1.01, 1.00 Tasks: 186 total, 2 running, 184 sleeping, 0 stopped, 0 zombie Cpu(s): 15.2%us, 14.6%sy, 0.1%ni, 70.0%id, 0.2%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 2074932k total, 1560840k used, 514092k free, 570160k buffers Swap: 4192956k total, 41480k used, 4151476k free, 598220k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 32522 csi135b7 25 0 4604 1104 972 R 100.0 0.1 1233:10 timer 15380 shs 15 0 2440 1016 748 R 2.0 0.0 0:00.01 top 1 root 15 0 2176 572 544 S 0.0 0.0 0:17.13 init 2 root RT -5 0 0 0 S 0.0 0.0 0:03.37 migration/0
In this top output, we see the memory stats in rows 4 and 5. The total installed memory along with how mush is in use and free are clearly labelled.
To look at shared memory on a Linux system, you can try the ipcs command. This command is available on both Linux and Solaris systems, but your options will vary.
$ ipcs -m ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x00000000 2818049 root 644 52 2 0x00000000 2850820 root 644 16384 2 0x00000000 2883589 root 644 268 2
With the -a option (the default), you'll see some additional information, though the system in this example shows that there are no semaphore arrays and message queuesin use.
$ ipcs -a ------ Shared Memory Segments -------- key shmid owner perms bytes nattch status 0x00000000 2818049 root 644 52 2 0x00000000 2850820 root 644 16384 2 0x00000000 2883589 root 644 268 2 ------ Semaphore Arrays -------- key semid owner perms nsems ------ Message Queues -------- key msqid owner perms used-bytes messages
Most Unix memory commands are faily easy to use and understand and frequent use will give you a feel for how your systems perform under normal circumstances and prepare you to notice when you system is in trouble.
Read more of Sandra Henry-Stocker's Unix as a Second Language blog and follow the latest IT news at ITworld, Twitter and Facebook.