Linuxfree command details and use instances (view memory usage)

Source: Internet
Author: User
The free command can display idle and used physical memory, swap memory, and buffer used by the kernel in Linux. Among the Linux system monitoring tools, the free command is one of the most frequently used commands.

1. command format:

Free [parameter]

2. command functions:

The free command displays system usage and idle memory, including physical memory, interactive zone memory (swap), and kernel buffer memory. The shared memory is ignored.

3. command parameters:

-B displays memory usage in bytes.

-K displays memory usage in KB.

-MB: memory usage is displayed in MB.

-G displays memory usage in GB.

-O does not display the buffer adjustment column.

-S <间隔秒数> Observe the memory usage continuously.

-T: displays the total memory column.

-V displays the version information.

4. example:

Instance 1: displays memory usage

Command:


Copy codeThe code is as follows:
Free
Free-g
Free-m

Output:


Copy codeThe code is as follows:
[Root @ SF1150 service] # free
Total used free shared buffers cached
Mem: 32940112 30841684 2098428 0 4545340 11363424
-/+ Buffers/cache: 14932920 18007192
Swap: 32764556 1944984 30819572


[Root @ SF1150 service] # free-g
Total used free shared buffers cached
Mem: 31 29 2 0 4 10
-/+ Buffers/cache: 14 17
Swap: 31 1 29


[Root @ SF1150 service] # free-m
Total used free shared buffers cached
Mem: 32168 30119 2048 0 4438 11097
-/+ Buffers/cache: 14583 17584
Swap: 31996 1899 30097

The free command output details:

The following is an explanation of these values:

Total: total physical memory size.

Used: used.

Free: available.

Shared: the total memory Shared by multiple processes.

Buffers/cached: disk cache size.

Row 3 (-/+ buffers/cached ):

Used: used.

Free: available.

The fourth line is SWAP partition SWAP, which is also known as virtual memory.

Difference: used/free of the second row (mem) differs from used/free of the third row (-/+ buffers/cache. The difference between the two is that from the perspective of usage, the first line is from the OS perspective, because for OS, buffers/cached is used, so its available memory is 2098428KB, the memory used is 30841684KB, including the + buffers + cached used by the kernel (OS) + Application (X, oracle, etc.

The third line indicates that, from the application perspective, for applications, buffers/cached is equivalent to available, because buffer/cached is designed to improve file read performance, when the application needs to use the memory, buffer/cached will be quickly recycled.

From the application perspective, the available memory = system free memory + buffers + cached.

The available memory for the local machine is:

18007156 = 2098428KB + 4545340KB + 11363424KB

Next, we will explain when the memory will be exchanged and by which side.

When the available memory is less than the rated value, a meeting will be held for exchange. how to view the rated value:

Command: cat/proc/meminfo

Output:


Copy codeThe code is as follows:
[Root @ SF1150 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: 0 Hugepagesize: 2048 kB

Switching reduces the number of physical pages used in the system in three ways:

1. reduce the buffer and page cache size,

2. swap out the V-type memory page of the system,

3. swap out or discard the page. (The Memory page occupied by the Application, that is, the physical memory is insufficient ).

In fact, using swap in a small amount affects the system performance.

So what is the difference between buffers and cached?

In order to improve disk access efficiency, Linux has made some careful designs, in addition to caching dentry (for VFS, accelerating the conversion of file path names to inode ), two major Cache methods are also adopted: Buffer Cache and Page Cache. The former is used to read and write disk blocks, and the latter is used to read and write inode files. These caches effectively shorten the time for I/O system calls (such as read, write, getdents.

Disk operations include logical level (file system) and physical level (disk block). These two types of Cache are logical and physical level data respectively.

The Page cache is actually for the file system, and is the file cache. data at the file level is cached in the page cache. The logic layer of the file needs to be mapped to the actual physical disk. this ing relationship is completed by the file system. When the page cache data needs to be refreshed, the data in the page cache is handed over to the buffer cache, because the Buffer Cache is the cache disk block. However, this kind of processing becomes simple after the kernel version 2.6, and there is no real cache operation.

Buffer cache is the cache for disk blocks, that is, if no file system is available, data directly operated on the disk will be cached in the buffer cache. for example, the metadata of the file system is cached in the 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, operations on the file will cache the data to the page cache. if you directly use dd or other tools to read and write the disk, the data will be cached to the buffer cache.

Therefore, in linux, as long as the swap space of swap is not used, we don't have to worry about having too little memory. if many swap instances are used, you may need to add physical memory. this is also the standard for linux to check whether the memory is sufficient.

If it is an application server, it generally only looks at the second row, + buffers/cache, that is, the free memory for the application is too small, it is also necessary to consider the optimization program or add memory.

Instance 2: displays memory usage information in the form of sum

Command: free-t

Output:


Copy codeThe code is as follows:
[Root @ SF1150 service] # free-t
Total used free shared buffers cached
Mem: 32940112 30845024 2095088 0 4545340 11364324
-/+ Buffers/cache: 14935360 18004752 Swap: 32764556 1944984 30819572 Total: 65704668 32790008 32914660 [root @ SF1150 service] #

Note:

Instance 3: periodically query memory usage information

Command: free-s 10

Output:


Copy codeThe code is as follows:
[Root @ SF1150 service] # free-s 10
Total used free shared buffers cached
Mem: 32940112 30844528 2095584 0 4545340 11364380
-/+ Buffers/cache: 14934808 18005304 Swap: 32764556 1944984 30819572
Total used free shared buffers cached
Mem: 32940112 30843932 2096180 0 4545340 11364388
-/+ Buffers/cache: 14934204 18005908 Swap: 32764556 1944984 30819572

Note:

Execute the command every 10 s

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.