performance / tuning tips. to the point.                
 
About Us | Site Map | Privacy
Disclaimer | Feedback
About RSS Feed | Useful Links
Search Partner Links
Original Blog
Add to My Yahoo!
Google Reader or Homepage
del.icio.us performancewiki.com Latest Items


© 2005-2009 PerformanceWiki.com
All Rights Reserved.


Analyzing and Tuning Memory Usage



  Linux  Windows  AIX  Solaris

Linux

To see if your system is short of physical memory, you can use /proc/meminfo and vmstat:

# ps aux --sort pmem

USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.0  1520  508 ?        S     2005   1:27 init
....
db2inst1  1309  0.0  0.4 344308 33048 ?      S     2005   1:55 db2agent (idle)
db2inst1  2919  0.0  0.4 345580 37368 ?      S     2005  20:02 db2agent (idle)
db2inst1 24594  0.0  0.4 345068 36960 ?      S     2005  15:45 db2agent (idle)
root     27645  0.0 14.4 1231288 1183976 ?   S     2005   3:01 /opt/WebSphere/AppServer/java/bin/java 
-Xboo tclasspath/p:/opt/WebSphere/AppServer/java/jre/lib/ext/ibmorb.jar:/opt/WebSphere/
AppServer/java/jre/lib/ext /ibmext.jar -Dwas.status.socket=56432 -classpath 
/opt/WebSphere/AppServer/properties:/opt/WebSphere/AppSer
--sort option outputs the highest %MEM at bottom.  Just note down the PID for the highest %MEM usage.

Then use 'ps ev --pid=<PID>' to show the change over time (you had to manually repeat it or put it as a cron to a file):

# ps ev --pid=27645

PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND
27645 ? S 3:01 0 25 1231262 1183976 14.4 /opt/WebSphere/AppServer/java/bin/java -Xbootclasspath
# ps ev --pid=27645

PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND
27645 ? S 3:01 0 25 1231262 1183976 14.4 /opt/WebSphere/AppServer/java/bin/java -Xbootclasspath
0
If RSS (resident set size, in KB) increases over time (so would %MEM), it may indicate a memory
leak in the application.

Windows

First, are you getting all the memory out of the system you paid for?  Many SMP machines not only come with mutiple CPUs (such as the IBM xSeries 330 and xSeries 366 with Intel® Xeon™), but also come with large physical memory (8GB for example).  But by default, the Windows 2003 server operating system only "sees" 4GB.  So make sure do the following:


Click on the 'Advanced' tab in System Properties.  Then click on the 'Startup and Recovery'
settings.  Edit the default operation system settings to have '/PAE /3GB' options.  Reboot
your system.  This is why the two options are needed for performance:
/PAE
Native 32-bit Intel x86 architecture allows 4GB memory space
The Intel Physical Address Extension (PAE) is 36-bit memory addressing mode
Allows memory address above 4GB (up to 64GB)
Many SMP machines come with more than 4GB RAM
Set in boot.ini as “/PAE”
/3GB
By default, Windows reserves 2GB for OS kernel (privileged-mode)
Leaving only 2GB (out of 4GB 32-bit addressable) for user-mode applications
“/3GB” in boot.ini reallocates 3GB for user-mode applications
Good for database servers with many agents running
Tests show generally 1GB for kernel enough
With unnecessary Windows services disabled

Windows Task Manager can be used to monitor your system.  You can start by "Start->Run->(type) taskmgr and hit <Enter>"


Click on the 'Processes' tab then click on the 'Mem Usage' column to sort so that the
largest memory-consuming application shows at top.  Identify the program's name and 
debug further.  Also, if Memory Usage keep increasing but not expectedly, your 
application may have a memory leak.  See "Code Tuning" for application performance
analysis.

To automate and capture memory usage into a file using 'perfmon' tool that comes with Windows 2003, see "perf automation" section from the left navigation.

AIX

First, use 'ps aux' to find out the program PID that you want to monitor memory usage.  Look in the %MEM column for the highest percentage:

# ps aux |more
USER         PID %CPU %MEM   SZ  RSS    TTY STAT    STIME  TIME COMMAND
root       57372 12.4  0.0   40   40      - A      Nov 28 8633:34 wait
root       90280  0.1  0.0  492  500      - A      Nov 28 55:12 /usr/sbin/syncd
root     1716352  0.0  8.0 1154040 1154072      - A      Dec 02  3:47 /usr/WebSphere/
db2inst1 1143036  0.0  0.0 1044  460      - A      Dec 02  2:11 db2disp 0
root           0  0.0  0.0   64   64      - A      Nov 28  4:55 swapper
db2inst1 1458184  0.0  0.0 9608 9636      - A      Dec 02  1:36 db2hmon 0
db2inst1 1409092  0.0  0.0 11960 6008      - A      Dec 02  1:15 db2agent (idle)
root      311516  0.0  0.0  908  968      - A      Nov 28  2:48 /usr/opt/db2_08
root      536650  0.0  1.0 204292 204324      - A      Nov 29  2:15 /usr/WebSphere/
root      290960  0.0  0.0  468  492      - A      Nov 28  2:18 /usr/sbin/getty
root       77862  0.0  0.0  116  116      - A      Nov 28  1:41 gil

then use 'svmon' to watch that PID's memory usage over a period of time (through the intervals and iterations parameters).  This tool not only can display memory usage for a program over time, but also detects possible memory leaks in kernel, a:

# svmon -P 1716352 -i 5 5 > ./mem.out
# grep -i "process" mem.out
[ Vsid        Esid Type Description		   LPage	 Inuse   Pin Pgsp Virtual ]
  37e236         2 work process private              s     48     3    0    48
  37e236         2 work process private              s     48     3    0    48
  37e236         2 work process private              s     48     3    0    48
  37e236         2 work process private              s     48     3    0    48
  37e236         2 work process private              s     48     3    0    48

The above command watches the WebSphere process for 5 iterations, 5 seconds each.  
If the column showing '48' (Inuse) keeps increasing over time, it may indicate a memory leak 
(means segment 'Esid' 2 is increasing). In this case, there is not leak.  This technique can 
be used with Java or C++ programs.

Column 'Esid' explanation:
Segment 0 (kernel) 	- a leak maybe in kernel itself, or device drivers.
Segment 2 (app) 		- user stack and data
Segment f (shared libs) 	- in standard libraries, etc.



Other very useful 'svmon' examples: 'svmon -G' to display global memory usage; 'svmon -P -t 1' to show the largest user (i.e., program) of memory:

# svmon -G
               size      inuse       free        pin    virtual
memory     41943040    3757231   38185809    2220261    2906150
pg space   82182144       6155

               work       pers       clnt
pin         2220261          0          0
in use      2906150          0     851081
# svmon -P -t 1 |more

-------------------------------------------------------------------------------
     Pid Command          Inuse      Pin     Pgsp  Virtual 64-bit Mthrd  16MB
  700600 java          268751844 268435502        0 268732920      N     Y     N

    Vsid      Esid Type Description              PSize  Inuse   Pin Pgsp Virtual
       0         0 work kernel (lgpg_vsid=0)         L  65536 65536    0 65536
  96c497         5 work working storage              s  65536     0    0 65536
 1f42bf5         4 work working storage              s  65536     0    0 65536
 1a5a1a4         3 work working storage              s  65507     0    0 65507
 1fd1fff         6 work working storage              s  52588     0    0 52588
  e30ee2         7 work working storage              s  33183     0    0 33183
  9d109d         d work shared library text          s  14610     0    0 14610
 1bd03bc         - clnt /dev/hd2:255351              s   1647     0    -     -
...

Solaris

'ps aux' can be used to determine %MEM used by programs:


# ps aux
to get a process-id for the highest memory-consuming application.
Processes have private memory to hold their stack space, heap, and data areas. The only 
way to see how much memory a process is actively using is to use 
/usr/proc/bin/pmap -x <process-id> 
which is available in Solaris 2.6 and later releases.