One Linux command per day (PNS): Free command

Source: Internet
Author: User

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.

-O does not display buffer throttling 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

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 29 2 0 4 10

-/+ buffers/cache:14 17

Swap:31 1 29

[Email protected]50 service]# free-m

Total used free shared buffers Cached

mem:32168 30119 2048 0 4438 11097

-/+ buffers/cache:14583 17584

swap:31996 1899 30097

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.

The third line refers to the application from the point of view, 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 be quickly 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 KB

memfree:2096700 KB

buffers:4545340 KB

cached:11364056 KB

swapcached:1896080 KB

active:22739776 KB

inactive:7427836 KB

hightotal:0 KB

highfree:0 KB

lowtotal:32940112 KB

lowfree:2096700 KB

swaptotal:32764556 KB

swapfree:30819572 KB

dirty:164 KB

writeback:0 KB

anonpages: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

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

3. Swap out or discard the page. (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.

So we look at Linux, as long as no swap swap space, 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 the memory is adequate.

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

-/+ 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

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

Execute one command per 10s

One Linux command per day (PNS): Free command

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.