How to correctly view the memory usage of Linux machines

Source: Internet
Author: User
Tags cpu usage

How to correctly view the memory usage of Linux machinesBackground

?? As long as the work on the Linux machine, there will basically be a need to see the memory usage, but how to look right? Previous use of the top command, there has been a misunderstanding.

Why does the top command look at memory with errors?

?? Top is a very useful system analysis tool that allows you to view processes, CPU usage, memory usage, and so on in real time, a bit like Task Manager under Windows. I used to think top saw the real memory usage, then baidugoogle long time, only to find their own patterns. = =||
First look at the top command to show the memory usage, I use a bricklayer VPS to do a demonstration:

Cpu(s):  0.0%us,  0.3%sy,  0.0%ni, 99.7%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%stMem:     73728k total,    70048k used,     3680k free,        0k buffersSwap:    16384k total,     4696k used,    11688k free,    64716k cached

?? You can see Mem: 73728k total, 70048k used, 3680k free, 0k buffers this line, which is memory usage. At first I looked at this line only. Later in the company's production machine on the look, feel very puzzled, as long as it is running for a period of time the machine, the total and used memory is always very close, the free value is very small, that is, "memory utilization" is very high, even if I have turned off all kinds of services. But even then, the server does not have any problems, start the Tomcat service again, do some small pressure test, the performance of the situation is very normal. Then the problem is: clearly the memory situation so tense, how can you run so many services, even do stress testing it? The answer is very simple, these seem to used a lot of memory, a large part of the cache , this is about the Linux memory management mechanism.

Cache Memory in Linux

?? What is cache memory (cached RAM):

When you read and write files, the Linux kernel caches the files in memory in order to improve read and write performance and speed, which is the memory (cache memory). The Cache memory will not be released automatically even after your program has finished running. This will cause you to read and write files frequently in your Linux system, and you will find that there is little physical memory available.
In fact, this cache memory is automatically released when you need to use memory, so you don't have to worry about not having memory available.
It is only possible that the application does not have enough memory to use if free subtracts the remaining portion of cached memory.

?? So in fact, the top saw the memory usage, part of the cache, how much is the cache? Note that there is a buffers behind the last mem line, and there is a cached behind the swap line, which is the cache size. So if you want to calculate how the application actually uses physical memory, it should be used-cached-buffers , so the physical memory usage that you just saw on top is 70048k-64716k=5332k. That's why it's useless!

?? In addition, if you simply want to see memory usage, using the free command is actually more intuitive:

             total       used       free     shared    buffers     cachedMem:         73728      70940       2788          0          0      64840-/+ buffers/cache:       6100      67628Swap:        16384       4500      11884

This information is described in the following general directions:
The first line describes the memory state used by the system in a global perspective:
total--Total Physical Memory
used--already uses memory, this value is generally larger because this value includes the memory used by the cache+ application
free--memory that is not fully used
shared--Application Shared Memory
buffers--cache, mainly used for directory aspects, Inode value, etc. (LS Large directory can see this value increase)
cached--cache, for files that have been opened
Pay attention to -/+ buffers/cache: 6100 67628 this line.
?? The previous value indicates that-buffers/cache-–> does not include caching, application physical memory usage, or-buffers/cache=used-buffers-cached, so the application uses 6100k of memory at this point.
?? The latter value indicates the amount of memory +buffers/cache-–> all available to the application, free plus the cached value, which is +buffers/cache=free+buffers+cached, so there is also 67628k of memory available for the program to use.
?? In addition, the free command can also use the "-m" parameter, so that the memory information displayed is in megabytes instead of KB, which is more intuitive in case of large memory.

free -m-----------             total       used       free     shared    buffers     cachedMem:            72         69          2          0          0         63-/+ buffers/cache:          5         66Swap:           16          4         11
Summarize

?? Using the top command or the free command to see the memory usage used, contains the cache, if you want to see the actual memory usage of the application, it should be used-cached-buffers, or directly see the results of the free command -/+ buffers/cache line information.

This article refers to
    1. About Linux Cache memory caches

How to correctly view the memory usage of Linux machines

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.