Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Linux system Command free

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to use the command free in the Linux system. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The main purpose of free command in Linux system is to display the status of memory, including physical memory, virtual swap file memory, shared memory segment and so on.

The use of the free command begins with the free-help command to see the usage of the free command.

Root@ubuntu116:~# free-- help Usage: free [options] Options:-b,-- bytes show output in bytes-k,-- kilo show output in kilobytes-m,-- mega show output in megabytes-g,-giga show output in gigabytes-- tera show output in terabytes-h -- human show human-readable output-- si use powers of 1000 not 1024-l,-- lohi show detailed low and high memory statistics-o,-- old use old format (without-/ + buffers/cache line)-t,-- total show total for RAM + swap-s N,-- seconds N repeat printing every N seconds-c N,-- count N repeat printing N times Then exit-- help display this help and exit-V,-- version output version information and exit For more details see free (1). 123456789101112131415161718192021222324 Let's test a few commonly used options

2.1 free-b

Root@ubuntu116:~# free-b

Total used free shared buffers cached

Mem: 67529682944 64533520384 2996162560 213880832 351289344 58317025280

-/ + buffers/cache: 5865205760 61664477184

Swap: 68690112512 7532544 68682579968

You have a new message in / var/mail/root

Root@ubuntu116:~#

2.2 free-k

Root@ubuntu116:~# free-k

Total used free shared buffers cached

Mem: 65946956 63518300 2428656 208900 343148 56952900

-/ + buffers/cache: 6222252 59724704

Swap: 67080188 7356 67072832

You have a new message in / var/mail/root

Root@ubuntu116:~#

2.3 free-m

Root@ubuntu116:~# free-m

Total used free shared buffers cached

Mem: 64401 62458 1942 204 335 55618

-/ + buffers/cache: 6505 57895

Swap: 65507 7 65500

Root@ubuntu116:~#

2.4 free-g

Root@ubuntu116:~# free-g

Total used free shared buffers cached

Mem: 62 60 1 0 0 54

-/ + buffers/cache: 6 56

Swap: 63 0 63

Root@ubuntu116:~#

2.5 free-h

Root@ubuntu116:~# free-h

Total used free shared buffers cached

Mem: 62G 60G 2.2G 204M 336M 54G

-/ + buffers/cache: 6.1g 56g

Swap: 63G 7.2M 63G

Root@ubuntu116:~#

2.6 free-l displays detailed low memory and high memory statistics

Root@ubuntu116:~# free-l

Total used free shared buffers cached

Mem: 65946956 63396736 2550220 209228 344632 56953528

Low: 65946956 63396736 2550220

High: 0 0 0

-/ + buffers/cache: 6098576 59848380

Swap: 67080188 7356 67072832

Root@ubuntu116:~#

2.7 free-o uses the old format (no line-/ + buffers/cache)

Root@ubuntu116:~# free-o

Total used free shared buffers cached

Mem: 65946956 64045184 1901772 209256 344760 56969912

Swap: 67080188 7356 67072832

Root@ubuntu116:~#

2.8 free-t shows the sum of RAM and swap

Root@ubuntu116:~# free-t

Total used free shared buffers cached

Mem: 65946956 64118724 1828232 209616 345348 56973588

-/ + buffers/cache: 6799788 59147168

Swap: 67080188 7356 67072832

Total: 133027144 64126080 68901064

Root@ubuntu116:~#

2.9 free-s N repeatedly prints memory information every N seconds until you press Ctrl + c

Root@ubuntu116:~# free-s 3

Total used free shared buffers cached

Mem: 65946956 64138880 1808076 209708 345616 56957708

-/ + buffers/cache: 6835556 59111400

Swap: 67080188 7356 67072832

Total used free shared buffers cached

Mem: 65946956 64073124 1873832 209716 345616 56957720

-/ + buffers/cache: 6769788 59177168

Swap: 67080188 7356 67072832

Total used free shared buffers cached

Mem: 65946956 64167628 1779328 209720 345616 56957732

-/ + buffers/cache: 6864280 59082676

Swap: 67080188 7356 67072832

Total used free shared buffers cached

Mem: 65946956 64159796 1787160 209720 345620 56957728

-/ + buffers/cache: 6856448 59090508

Swap: 67080188 7356 67072832

Total used free shared buffers cached

Mem: 65946956 64166776 1780180 209724 345628 56957740

-/ + buffers/cache: 6863408 59083548

Swap: 67080188 7356 67072832

Total used free shared buffers cached

Mem: 65946956 63093948 2853008 209724 345632 56957756

-/ + buffers/cache: 5790560 60156396

Swap: 67080188 7356 67072832

2.10 free-c N repeatedly print memory information N times, then exit

Root@ubuntu116:~# free-c 3

Total used free shared buffers cached

Mem: 65946956 64199120 1747836 209780 345744 56974404

-/ + buffers/cache: 6878972 59067984

Swap: 67080188 7356 67072832

Total used free shared buffers cached

Mem: 65946956 63949560 1997396 209780 345744 56974404

-/ + buffers/cache: 6629412 59317544

Swap: 67080188 7356 67072832

Total used free shared buffers cached

Mem: 65946956 64093104 1853852 209780 345744 56974408

-/ + buffers/cache: 6772952 59174004

Swap: 67080188 7356 67072832

Root@ubuntu116:~#

2.11 free-V displays version number information

Root@ubuntu116:~# free-V free from procps-ng 3.3.9 root@ubuntu116:~# III. Explanation of nouns in the output of the free command

Root@ubuntu116:~# free

Total used free shared buffers cached

Mem: 65946956 64255040 1691916 213840 356892 56805760

-/ + buffers/cache: 7092388 58854568

Swap: 67080188 7356 67072832

Root@ubuntu116:~#

3.1 Mem,-/+ buffers/cache,Swap explains that we see that the free command has three lines: Mem,-/+ buffers/cache,Swap, which represents the following:

The content represented by the name is Mem memory usage-/ + buffers/cache indicates how much physical memory has been used and how much Swap swap space can be used 3.2 total,used,free,shared,buffers,cached explanation column is total,used,free,shared,buffers,cached, which is represented as follows:

The name represents the total amount of content total used has used free free shared shared, there is a lot of shared memory in linux, such as a libc library, a lot of program calls, but actually only a copy of buffers cache, can be recycled cached cache, can be recycled on the difference between buffers and cached here there is a place to pay attention to the difference between buffers and cached. With regard to the difference between cached and buffers in the free command, the following picture can be expressed: (the picture is quoted from https://blog.csdn.net/juS3Ve/article/details/79191634 Song Baohua's blog)

Both cached and buffers can read and write, the only difference between them is that buffers has a bare device or partition as the background, and cached has a file in the file system as the background.

For example, cat / dev/sda1 > / dev/null, then the content of / dev/sda1 goes into buffers, and if cat / opt/test.py > / dev/null, the content of / opt/test.py goes into cached.

Sometimes the words buffer and cache are often mixed. However, in some lower-level software, it is necessary to distinguish between these two words, look at the foreign language of foreigners:

A buffer is something that has yet to be "written" to disk.

A cache is something that has been "read" from the disk and stored for later use. In other words, buffer is used to store data to be output to disk (block device), while cache is used to store data read from disk. Both are designed to improve IO performance and are managed by OS.

Linux and other mature operating systems (such as windows) always need to cache more data in order to improve the performance of IO read, which is why the cached of Mem is larger and the free of Mem is smaller.

4. Free Command output content calculation Company next let's calculate how the parameters of the free command are calculated, refer to the picture below (the picture is quoted from https://blog.csdn.net/juS3Ve/article/details/79191634 Song Baohua's blog)

4.1Formula for calculating total in the line Mem is: total = used + free

Total (65946956) = used (64255040) + free (1691916) for example, in this case, the used of Mem is 64255040 and the free is 1691916, which adds up to 65946956 of total.

The second line of free output is the use of system memory from an application's point of view.

The used on the line-/ + buffers/cache, namely-buffers/cache, indicates how much memory an application thinks the system is using and actually eats.

The formula is-/ + buffers/cache 's used = Mem's used-Mem's buffers-Mem's cached.

Used (7092388) of `- / + buffers/cache = used (64255040) of Mem-buffers (356892) of Mem-cached of Mem (56805760)

For example, in this case, the used of-/ + buffers/cache is 7092388, which is determined by the used 64255040 of the Mem line, minus the buffers 356892 of the Mem line, and the cached 56805760 of the Mem line.

4.3Formula for calculating free on the line-/ + buffers/cache-free on the line + buffers/cache, that is, + buffers/cache, indicates how much memory an application thinks the system has left and the total amount of memory that can be misappropriated

The formula is-/ + buffers/cache 's free = Mem's free + Mem's buffers + Mem's cached.

-/ + free (58854568) of buffers/cache = free (1691916) of Mem + buffers (356892) of Mem + cached of Mem (56805760)

For example, in this example, the free of-/ + buffers/cache is 58854568, which is the value of free 1691916 in the line Mem, plus buffers 356892 in the line Mem, and cached 56805760 in the line Mem.

Because the memory consumed by the system cache and buffer can be quickly reclaimed, the free of the line-/ + buffers/cache is usually much larger than the free of the line Mem.

4.4.The formula for calculating total in the line Swap is: total = used + free

Total (67080188) = used (7356) + free (67072832) for example, in this case, the used of Swap is 7356 and the free is 67072832, which adds up to exactly 67080188 of total.

You can refer to the free value of-/ + buffers/cache when estimating free physical memory, but the actual available memory is less than this value because not all buffers and cached can be freed.

I think everyone is still dizzy when they look up there. Why are used and free so strange in the results of the first part (Mem) and the second part (- / + buffers/cache)?

In fact, we can explain it from two aspects.

For the operating system, the buffers/cached of the Mem line belongs to being used, so it thinks that free is only 1691916.

For the application, it is-/ + buffers/cach. Buffers/cached is equally available because buffer/cached is designed to improve the performance of program execution, and buffer/cached is quickly used when the program uses memory.

What is buffer/cache about buffer and cache5.1? Buffer and cache are two nouns that are overused in computer technology, and they have different meanings in an incomprehensible context.

In Linux memory management, the buffer here refers to Linux memory: Buffer cache. The cache here refers to the: Page cache in the Linux memory. Translated into Chinese, it can be called buffer cache and page cache. Historically, one of them (buffer) has been used as a cache for writes to io devices, while the other (cache) has been used as a read cache for io devices, where io devices mainly refer to block device files and normal files on the file system. But now, their meaning is different. In the current kernel, page cache, as its name implies, is a cache for memory pages. To put it bluntly, if memory is allocated and managed by page, it can be managed using page cache as its cache. Of course, not all memory is managed by page, and a lot of memory is managed by block. If you want to use the cache function, this part of the memory usage is concentrated in buffer cache. (from this point of view, would it be better for buffer cache to change its name to block cache? However, not all block have a fixed length, and the length of the block on the system is mainly determined by the block device used, while the page length on X86 is 4k either 32-bit or 64-bit.

Knowing the difference between these two caching systems, you can understand what they can do.

What is page cachePage cache is mainly used as a cache of file data on the file system, especially when the process has read / write operations on files. If you think about it, as a system call that maps files to memory: isn't it natural that mmap should also use page cache? In the current system implementation, page cache is also used as a cache device for other file types, so in fact page cache is also responsible for caching most of the block device files.

What is buffer cacheBuffer cache is mainly designed to be used by a system that caches blocks when the system reads or writes to block devices. This means that some operations on blocks are cached using buffer cache, such as when we format the file system. In general, two cache systems are used together, for example, when we write to a file, the contents of page cache are changed, while buffer cache can be used to mark page as different buffers and record which buffer has been modified. In this way, when the kernel performs a writeback of dirty data later, it does not have to write back the entire page, but only needs to write back the modified part.

5.4 how to recycle cache? The Linux kernel triggers memory collection when memory is about to be exhausted to free up memory for processes that need it. In general, the main memory release in this operation comes from the release of buffer / cache. Especially when more cache space is used. Since it is mainly used for caching, only to speed up the process of reading and writing files when there is enough memory, then in the case of high memory pressure, it is of course necessary to free cache as free space for the relevant processes to use. So in general, we think that buffer/cache space can be released, which is correct.

But this kind of caching work is not without cost. If you understand what cache does, you can understand that clearing the cache must ensure that the data in the cache is consistent with the data in the corresponding file before the cache can be released. Therefore, the behavior of cache clearance is generally accompanied by high system IO. Because the kernel needs to compare whether the data in the cache is consistent with the data on the corresponding hard disk file, if it is inconsistent, it needs to be written back before it can be reclaimed.

Manually triggered cache cleanup

In addition to clearing the cache when memory is about to be exhausted in the system, we can also use the following file to manually trigger the cache cleanup operation:

[root@tencent64 ~] # cat / proc/sys/vm/drop_caches method is:

Echo 1 > / proc/sys/vm/drop_caches of course, this file can be set to values of 1, 2, and 3, respectively. What they mean is:

Echo 1 > / proc/sys/vm/drop_caches means to clear pagecache.

Echo 2 > / proc/sys/vm/drop_caches means to clear objects (including directory item cache and inode cache) from the recycled slab allocator. The slab allocator is a mechanism for managing memory in the kernel, and many of the cached data implementations use pagecache.

Echo 3 > / proc/sys/vm/drop_caches means to clear the cache objects in the pagecache and slab allocators.

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

On the use of Linux system command free to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report