The free command displays the idle, used physical memory and swap memory in the Linux system, and the buffer used by the kernel. In Linux system monitoring tools, the free command is one of the most frequently used commands.
1. Command format:
free [parameters]
2. Command function?:
The free command displays system usage and idle memory conditions, including physical memory, interactive area memory (swap), and kernel buffer memory. Shared memory will be ignored
3. Command parameters:
-B Displays memory usage in bytes.
-K displays memory usage in kilobytes.
-m displays memory usage in megabytes.
-G displays memory usage in gigabytes.
#p # Page Header #e#-o does not display buffer-adjusted columns.
-s< interval seconds > Continuous observation of memory usage.
-T displays the memory sum column.
-V Displays version information.
4. Usage examples:
Example 1: Show memory usage
Command:
Free
Free-g
Free-m
Output:
[[email protected] service]# free#p# page title #e#
Total used free shared buffers Cached
mem:32940112 30841684 2098428 0 4545340 11363424
-/+ buffers/cache:14932920 18007192
swap:32764556 1944984 30819572
[Email protected] service]# free-g
Total used free shared buffers Cached
Mem:31 2 0 4 10#p# page title #e#
-/+ buffers/cache:14 17
Swap:31 1 29
[Email protected] service]# free-m
Total used free shared buffers Cached
mem:32168 30119 2048 0 4438 11097
-/+ buffers/cache:14583 17584
swap:31996 1899 30097
#p # Page Title #e# Description:
Here is an explanation of these values:
Total: The amount of physical memory.
Used: How large is used.
Free: How much is available.
Shared: The total amount of memory shared by multiple processes.
Buffers/cached: The size of the disk cache.
Third line (-/+ buffers/cached):
Used: How large is used.
Free: How much is available.
Line four is swap partition swap, which is what we usually call virtual memory.
Difference: The used/free of the second line (MEM) differs from the third row (-/+ Buffers/cache) used/free. The difference between the two is that the first line is from the OS point of view, because for the os,buffers/cached are all belong to be used, so his available memory is 2098428KB, the used memory is 30841684KB, which includes, the kernel (OS) uses + Application (X, ORACLE,ETC) uses the +buffers+cached.
#p # page Title #e# The third line refers to the application, buffers/cached is equal to the availability of the application, because buffer/cached is to improve the performance of the file read, when the application needs to use memory, buffer/ Cached will soon be recycled.
So from the application's point of view, available memory = System Free memory+buffers+cached.
If the available memory for this machine condition is:
18007156=2098428kb+4545340kb+11363424kb
Next, explain when the memory will be exchanged, and by what side.
When the available memory is less than the rated value, a meeting is exchanged. How to see the rating:
Command:
Cat/proc/meminfo
Output:
[Email protected] service]# Cat/proc/meminfo
memtotal:32940112 #p # page Title #e#kb
memfree:2096700 KB
buffers:4545340 KB
cached:11364056 KB
swapcached:1896080 KB
active:22739776 KB
inactive:7427836 KB
hightotal:0 KB
#p # pagination Title #e#highfree:0 KB
lowtotal:32940112 KB
lowfree:2096700 KB
swaptotal:32764556 KB
swapfree:30819572 KB
dirty:164 KB
writeback:0 KB
Anonpages: #p # page Title #e#14153592 KB
mapped:20748 KB
slab:590232 KB
pagetables:34200 KB
nfs_unstable:0 KB
bounce:0 KB
commitlimit:49234612 KB
committed_as:23247544 kb#p# pagination Title #e#
vmalloctotal:34359738367 KB
vmallocused:278840 KB
vmallocchunk:34359459371 KB
hugepages_total:0hugepages_free:0hugepages_rsvd:0hugepagesize:2048 KB
The exchange will reduce the number of physical pages used in the system in three ways:
1. Reduce the buffer and the size of the page cache,
2. Swap out the memory page of the System V type
#p # page title #e#3. Swap out or discard pages. (Application occupies a memory page, that is, insufficient physical memory).
In fact, a small amount of swap use does not affect the performance of the system.
That buffers and cached are caches, what's the difference between them?
In order to improve disk access efficiency, Linux has done some careful design, in addition to the Dentry cache (for VFS, speed up the file path name to Inode conversion), but also adopted two main cache mode: Buffer cache and Page cache. The former is for the disk block read and write, the latter for the file inode read and write. These caches effectively shorten the time for I/O system calls (such as read,write,getdents).
The operations of a disk are logical (file system) and physical (disk blocks), which are caches of logical and physical-level data, respectively.
The page cache is actually for the file system, the file cache, and the data at the file level is cached to page cache. The logical layer of the file needs to be mapped to the actual physical disk, and this mapping is done by the file system. When the page cache data needs to be refreshed, the data in the page cache is given to buffer cache because the buffer cache is the cached disk block. But this kind of processing after the 2.6 version of the kernel has become very simple, there is no real sense of the cache operation.
Buffer cache is a caching of disk blocks that, in the absence of a filesystem, caches data directly into the buffer cache, for example, the file system's metadata is cached in buffer cache.
In short, page cache is used to cache file data, and buffer cache is used to cache disk data. In the case of a file system, the data is cached to the page cache, and if the disk is read and written directly using tools such as DD, the data is cached to buffer cache.
#p # page Title #e# so we look at Linux, as long as we do not swap space swap, there is no need to worry about their memory too little. If you use a lot of swap, you might want to consider adding physical memory. This is also the standard for Linux to see if memory is sufficient.
In the case of an application server, the general only looks at the second line, +buffers/cache, which is too little memory for the application, and it is time to consider optimizing the program or adding memory.
Example 2: Displaying memory usage information as a sum
Command:
Free-t
Output:
[Email protected] service]# free-t
Total used free shared buffers Cached
mem:32940112 30845024 2095088 0 4545340 11364324#p# page title #e#
-/+ buffers/cache:14935360 18004752swap:32764556 1944984 30819572total:65704668 32790008 32914660[[ Email protected] service]#
Description
Example 3: Periodic query Memory usage information
Command:
Free-s 10
Output:
[Email protected] service]# Free-s 10
#p # pagination title #e# total used free shared buffers Cached
mem:32940112 30844528 2095584 0 4545340 11364380
-/+ buffers/cache:14934808 18005304swap:32764556 1944984 30819572
Total used free shared buffers Cached
mem:32940112 30843932 2096180 0 4545340 11364388
-/+ buffers/cache:14934204 18005908swap:32764556 1944984 30819572
Description
#p # pagination Header #e# every 10s command
Reproduced in: http://www.itxuexiwang.com/a/liunxjishu/2016/0306/249.html?1457360628
One Linux command per day: free command