Memory Usage check

Hello Friends,

I need to check memory usage & availability before I could run a program if there is enough memory is left or not, so how could i achieve this? Which command output i should rely on? I have diplayed outputs of SAR, VMstat and PRstat commands below, But how could i check memory usage according to total mem?

Output of SAR command for Free & Swap Mem.

-bash-3.00$ sar -r 4 5 

SunOS dx352 5.10 Generic_147441-10 i86pc    05/26/2014

16:10:56 freemem freeswap
16:11:00 6541752 61892086
16:11:04 6541762 61892824
16:11:08 6541676 61892808
16:11:12 6541589 61892808
16:11:16 6541410 61892808

Average  6541638 61892667

For example i have tried this directingSAR output to a file, however i can't compare it to total memory. How could i proceed? should i use "prtconf | grep -i mem" output to compare the value below?

cat 123.txt | nawk '/Average/{print $2}'

Output of VMSTAT for Memoru Usage Status of Virtual Machine

-bash-3.00$ vmstat 4 5
 kthr      memory            page            disk          faults      cpu
 r b w   swap  free  re  mf pi po fr de sr s0 s1 s2 --   in   sy   cs us sy id
 0 0 0 37855508 28701468 31 49 49 0 0 0  0 -0  4 -0  0 2247 2054 1087  0  1 98
 0 0 0 30946784 26165180 3 14 0 0  0  0  0  0  6  0  0 2798 2554 1497  1  0 99
 0 0 0 30946632 26164856 0 4 0  0  0  0  0  0  3  0  0 2927 3488 1825  1  2 97
 0 0 0 30946632 26164308 0 2 0  0  0  0  0  0 12  0  0 5979 8694 4221  2  2 96
 0 0 0 30946632 26164040 0 0 0  0  0  0  0  0  4  0  0 3630 4079 2333  1  1 98

Output of PRSTAT command which give process status based on users.

-bash-3.00$ prstat -u sscm -a -s cpu 0 1 | sed -e '/^$/d;/sleep/d;/Total/d'
   PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/NLWP       
 26306 sscm     3492K 2668K cpu7    59    0   0:00:00 0.0% prstat/1
 NPROC USERNAME  SWAP   RSS MEMORY      TIME  CPU                             
    13 sscm       25G 8581M    17%  69:43:21 1.3%

In the output of vmstat the free memory is in KB, the sar output is in pages.

bash-3.2$ sar -r 1 1

SunOS <name> 5.10 Generic_147440-09 sun4v    05/26/2014

14:23:00 freemem freeswap
14:23:01 10776773 203955648
bash-3.2$ pagesize
8192
bash-3.2$ echo '10776773*8/1024' | bc
84193
bash-3.2$ vmstat 1 1
 kthr      memory            page            disk          faults      cpu
 r b w   swap  free  re  mf pi po fr de sr lf s0 s4 s5   in   sy   cs us sy id
 0 0 0 101746080 85650520 267 1364 0 0 0 0 0 0 -0 -0 7 5002 17103 5156 0  0 99

The above shows 81/82GB of free ram.
You're running Solaris x86, so you have a pagesize of 4K:

$ echo '6541638*4/1024' | bc
25553
$ echo 26164040/1024 | bc
25550

EDIT: Corrected, see jlliagre's post below. The above output shows 24GB of free RAM.

Use /usr/sbin/prtconf|fgrep Memory to check the total installed RAM.

1 Like

You might also want to check echo "::memstat" | mdb -k .

ZFS tends to use free memory on the system (if not limited), but it will free it as soon as some process requires it.

I did not know this command before but it really looks good to me.

I could run it as root user and it gave me an output like the following, however i may not be allowed to use root user permission to run such commands in production. What is the difference between free list and cache list? shall i consider the %Tot column for "Free" page sum?

-bash-3.00# echo "::memstat" | mdb -k
Page Summary                Pages                MB  %Tot
------------     ----------------  ----------------  ----
Kernel                     526621              2057    4%
Anon                       972918              3800    8%
Exec and libs              159977               624    1%
Page cache                3136595             12252   25%
Free (cachelist)          3495645             13654   28%
Free (freelist)           4286828             16745   34%

Total                    12578584             49135
Physical                 12250366             47852

You have plenty of RAM available (62%), the freelist is unused RAM and contains nothing of interest while the cachelist, despite also being unused RAM contains data from previously accessed files. If these files are read again, the data will be picked from that memory saving I/Os.

Well, I suppose the output of sar/vmstat and mdb are not sampled at the same time (or they are from different systems).
I'd expect the freelist (mdb) and free (vmstat) to be almost equal ...,
or I'm missing something?

Out of ~49 GB, there is 26 GB or free RAM in the first posting (sar/vmstat) and 30 GB in the second one (::memstat), not that much a difference.

1 Like

Thanks,
I've corrected my post above.

This is from a Nagios plugin script:

swap -s | nawk '{print int($9*100/($11+$9))}'

It displays the used virtual memory in percent.