“sar –r” can help you to track memory usage variation roughly. But it doesn’t mean it has memory leak if the memory used reached >90%. We need use free/top and pmap tool for more investigation. Here is the free tool introduction:
- Cached memory is essentially free, in that it can be replaced quickly if a running (or newly starting) programs needs the memory. The ‘cache’ or ‘buffer’ can be reused if we need memory.
- The-/+ buffers/cache line shows how much memory is used and free from the perspective of the applications. We usethis line to identify if there is a actual memory leak in the system.
- Themem line shows the memory usage from OS level. From the application view, theactual used memory(1108)=used(7797)-buffers(498)-cached(6189)
- We can use “free –k –s <delay>” or “free –m –s <delay>” to help catch the memory usage info which will help you to identify if there is memory leak.
- If there is swap memory used in the realtime system, please pay attention to it, maybe has impact to performance.
- The disk activity like copy files will increase the cache usage.
SYNOPSIS
usage: free [-b|-k|-m|-g] [-l] [-o] [-t] [-s delay] [-c count] [-V]
xx35-0-0-9:/root-#free –m –s 10
|
total |
used |
free |
shared |
buffers |
cached |
Mem: |
7861 |
7797 |
64 |
0 |
498 |
6189 |
-/+ buffers/cache: |
|
1108 |
6753 |
|
|
|
Swap: |
4110 |
0 |
4110 |
|
|
|
|
|
|
|
|
|
|
total |
|
used |
free |
shared |
buffers |
cached |
Mem: |
7861 |
7777 |
84 |
0 |
498 |
6189 |
-/+ buffers/cache: |
|
1088 |
6772 |
|
|
|
Swap: |
4110 |
0 |
4110 |
|
|
|
The disk buffer stores recently accessed disk data in memory; if the same data is needed again it can be quickly retrieved from the cache, improving performance.
Sample that copy the same file twice, the time decreased dramatically.
bjlinc1079 >ls -ltr *.out ßwe have a 300M file
-rw-rw-r-- 1 canhuali bicbj354148300 Dec 20 2010 Multi-IMSI_All.out
bjlinc1079 >free
total used free shared buffers cached
Mem: 8168704 5206884 2961820 0 167080 429112 ßcached memory is 429M. OS level view, only 2.9G memory is free
-/+ buffers/cache: 4610692 3558012 ß application level view,3.5G memory is free
Swap: 2040244 1732800 307444
bjlinc1079 >time cp Multi-IMSI_All.out Multi-IMSI_All.out.old ßthe first copy cost 20.69s
0.009u 0.804s0:20.69 3.8% 0+0k 0+0io 1pf+0w
bjlinc1079 <27:canhuali>free
total used free shared buffers cached
Mem: 8168704 5732540 2436164 0 167688 946800 ßcached memory increased to 946M
-/+ buffers/cache: 4618052 3550652
Swap: 2040244 1732800 307444
bjlinc1079 >time cp Multi-IMSI_All.out Multi-IMSI_All.out.old2 ßthe 2nd copy only cost 0.74s. Use the cache, it speed the copy.
0.007u 0.711s0:00.74 95.9% 0+0k 0+0io 0pf+0w
bjlinc1079 >
The “total” memory is the amount available after loading the kernel.
Any memory being used for processes or disk buffering is listed as “used.”
Memory that is currently unused is listed in the “free” column.
Note that the total memory is equal to the sum of the “used” and “free” columns.
total=free+used
The memory indicated as “shared” is an indication of how much memory is common to more than one process. A program such as the shell typically has more than one instance running. The executable code is read-only and can be shared by all processes running the shell.
The “buffers” entry indicates how much of the memory in use is currently being used for disk buffering.
The “free” command also shows very clearly whether the swap space is enabled, and how much swapping is going on.
The difference between buffers and cache
Buffers are associated with a specific block device, and cover caching of file system metadata as well as tracking in-flight pages. The cache only contains parked file data. That is, the buffers remember what's in directories, what file permissions are, and keep track of what memory is being written from or read to for a particular block device. The cache only contains the contents of the files themselves.
The difference among VIRT, RES, and SHR in top output
VIRT stands for the virtual size of a process, which is the sum of memory it is actually using, memory it has mapped into itself (for instance the video card's RAM for the X server), files on disk that have been mapped into it (most notably shared libraries), and memory shared with other processes. VIRT represents how much memory the program is able to access at the present moment.
RES stands for the resident size, which is an accurate representation of how much actual physical memory a process is consuming. (This also corresponds directly to the %MEM column.) This will virtually always be less than the VIRT size, since most programs depend on the C library.
SHR indicates how much of the VIRT size is actually sharable memory or libraries). In the case of libraries, it does not necessarily mean that the entire library is resident. For example, if a program only uses a few functions in a library, the whole library is mapped and will be counted in VIRT and SHR, but only the parts of the library file containing the functions being used will actually be loaded in and be counted under RES.