如何可靠地测量Linux中的可用内存?

时间:2021-12-09 08:57:30

Linux /proc/meminfo shows a number of memory usage statistics.

Linux /proc/meminfo显示了一些内存使用统计信息。

MemTotal:      4040732 kB
MemFree:         23160 kB
Buffers:        163340 kB
Cached:        3707080 kB
SwapCached:          0 kB
Active:        1129324 kB
Inactive:      2762912 kB

There is quite a bit of overlap between them. For example, as far as I understand, there can be active page cache (belongs to "cached" and "active") and inactive page cache ("inactive" + "cached").

它们之间有一些重叠。例如,据我所知,可以有活动页面缓存(属于“缓存”和“活动”)和非活动页面缓存(“非活动”+“缓存”)。

What I want to do is to measure "free" memory, but in a way that it includes used pages that are likely to be dropped without a significant impact on overall system's performance.

我想要做的是测量“空闲”内存,但是在某种程度上,它包含了可能会被删除而不会对整个系统性能产生重大影响的已使用页面。

At first, I was inclined to use "free" + "inactive", but Linux's "free" utility uses "free" + "cached" in its "buffer-adjusted" display, so I am curious what a better approach is.

一开始,我倾向于使用“free”+“inactive”,但是Linux的“free”实用程序在它的“buffer-adjusted”显示中使用“free”+“cached”缓存,所以我很好奇哪种更好的方法。

When the kernel runs out of memory, what is the priority of pages to drop and what is the more appropriate metric to measure available memory?

当内核耗尽内存时,要删除的页面的优先级是多少?度量可用内存的更合适的指标是什么?

5 个解决方案

#1


8  

Since what “available memory” precisely means depends on your purpose, and your purpose is to avoid OOM situations:

因为“可用内存”的确切含义取决于您的目的,而您的目的是避免OOM情况:

Check out how Qt Extended (previously Qtopia) anticipates OOM situations.

检查Qt扩展(之前的Qtopia)如何预期OOM的情况。

There are two events:

有两个事件:

  • (MemFree + Buffers + Cached) / MemTotal < treshold (in /proc/meminfo)
  • (MemFree + buffer + Cached) / MemTotal < treshold (in /proc/meminfo)
  • Major pagefaults > treshold (pgmajfault in /proc/vmstat I think)
  • 主要的pagefaults > treshold(我认为是/proc/vmstat中的pgmajfault)

The first is an early warning that memory is low, and triggers more frequent monitoring of pagefaults. The second signals trashing, which kills system performance and is a good hint the OOM killer will run.

第一个是对内存不足的早期警告,并触发对pagefaults更频繁的监视。第二个信号是垃圾化,这将破坏系统的性能,并且很好地暗示了OOM杀手将会运行。

#2


3  

At first I found your question easy, as in practice output from free in the columns '+ buffers/cache' is the one I use and it normally works.

一开始我觉得你的问题很简单,因为在实践中,“+ buffer /cache”列中的“free”输出是我使用的,它通常是有效的。

But one of the situations in which it does not work is when you have heavy read to the same blocks. E.g. reading the same 1 gb_file over and over:

但是当你对同样的块进行了大量的阅读时,它就失效了。例如,反复阅读相同的1 gb_file:

while true; do cat 1gb_file >/dev/null; done

If your system has > 1 GB cache then this will run fast. But if you start using some of that cache for something else it will smash the performance of the system.

如果您的系统有>1gb的缓存,那么它将运行得很快。但是如果你开始使用某些缓存,它会破坏系统的性能。

So when you evaluate your solutions try the above and see if the solution takes that into account.

所以当你评估你的解的时候试试上面的,看看解是否考虑到它。

#3


2  

I'd say it's hard to measure which pages, when dropped, would cause the system to have a "significant impact on overall system performance". Pages in use by the user processes are would be the (Total) - (Free + Cached + Paged). The 2nd term is all the memory the kernel could free if required. However, freeing memory pages being used for cache and pages would have a significant impact on overall system performance.

我想说的是,很难衡量哪些页面在删除后会导致系统“对整个系统性能产生重大影响”。用户进程使用的页面将是(总数)-(免费+缓存+分页)。第二项是内核在需要时可以释放的所有内存。但是,释放用于缓存和页面的内存页将对整个系统性能产生重大影响。

If I were going to use a heuristic, I'd say that you should take the value of "Inactive" which is "The total amount of buffer or page cache memory, in kilobytes, that are free and available. This is memory that has not been recently used and can be reclaimed for other purposes." If you find that you take that and the system continues to work fine, you could estimate some percentage of "Active" to take as a guess as well because the system might have used some pages recently but isn't going to use them again. You know more about the system than me. If the system is dedicated to whatever you're about to do, then the pages and cached files in Active that may be used soon would depend on whether the system was recently used for something else.

如果我要用启发式,我会说你应该取“非活动”的值,即“缓冲或页缓存内存的总量,以千字节为单位,是*可用的”。这是最近没有使用的内存,可以用于其他用途。如果您发现您使用了它,并且系统继续正常工作,您可以估计“Active”的百分比作为猜测,因为系统可能最近使用了一些页面,但是不会再使用它们。你比我更了解这个系统。如果系统专门用于您将要做的任何事情,那么可能很快使用的活动中的页面和缓存文件将取决于系统最近是否用于其他事情。

#4


2  

I use the following:

我使用下面的:

FREE_KB = MemFree + Buffers + Cached

FREE_KB=$(($(echo `sed -n '2p;3p;4p' <  /proc/meminfo | sed "s/ \+/ /g" | cut -d' ' -f 2 ` | sed "s/ /+/g")))


USED_KB = MemTotal - MemFree - Buffers - Cached

USED_KB=$(($(echo `head -n4 /proc/meminfo | sed "s/ \+/ /g" | cut -d' ' -f 2 ` | sed "s/ /-/g")))

#5


2  

From linux-3.14 there is new metric MemAvailable in /proc/meminfo.

从linux-3.14中可以在/proc/ meminfo中找到新的memmetric。

And check line '-/+ buffers/cache:' in output of utility free.

并检查行'-/+缓冲区/缓存:'在无实用程序输出。

#1


8  

Since what “available memory” precisely means depends on your purpose, and your purpose is to avoid OOM situations:

因为“可用内存”的确切含义取决于您的目的,而您的目的是避免OOM情况:

Check out how Qt Extended (previously Qtopia) anticipates OOM situations.

检查Qt扩展(之前的Qtopia)如何预期OOM的情况。

There are two events:

有两个事件:

  • (MemFree + Buffers + Cached) / MemTotal < treshold (in /proc/meminfo)
  • (MemFree + buffer + Cached) / MemTotal < treshold (in /proc/meminfo)
  • Major pagefaults > treshold (pgmajfault in /proc/vmstat I think)
  • 主要的pagefaults > treshold(我认为是/proc/vmstat中的pgmajfault)

The first is an early warning that memory is low, and triggers more frequent monitoring of pagefaults. The second signals trashing, which kills system performance and is a good hint the OOM killer will run.

第一个是对内存不足的早期警告,并触发对pagefaults更频繁的监视。第二个信号是垃圾化,这将破坏系统的性能,并且很好地暗示了OOM杀手将会运行。

#2


3  

At first I found your question easy, as in practice output from free in the columns '+ buffers/cache' is the one I use and it normally works.

一开始我觉得你的问题很简单,因为在实践中,“+ buffer /cache”列中的“free”输出是我使用的,它通常是有效的。

But one of the situations in which it does not work is when you have heavy read to the same blocks. E.g. reading the same 1 gb_file over and over:

但是当你对同样的块进行了大量的阅读时,它就失效了。例如,反复阅读相同的1 gb_file:

while true; do cat 1gb_file >/dev/null; done

If your system has > 1 GB cache then this will run fast. But if you start using some of that cache for something else it will smash the performance of the system.

如果您的系统有>1gb的缓存,那么它将运行得很快。但是如果你开始使用某些缓存,它会破坏系统的性能。

So when you evaluate your solutions try the above and see if the solution takes that into account.

所以当你评估你的解的时候试试上面的,看看解是否考虑到它。

#3


2  

I'd say it's hard to measure which pages, when dropped, would cause the system to have a "significant impact on overall system performance". Pages in use by the user processes are would be the (Total) - (Free + Cached + Paged). The 2nd term is all the memory the kernel could free if required. However, freeing memory pages being used for cache and pages would have a significant impact on overall system performance.

我想说的是,很难衡量哪些页面在删除后会导致系统“对整个系统性能产生重大影响”。用户进程使用的页面将是(总数)-(免费+缓存+分页)。第二项是内核在需要时可以释放的所有内存。但是,释放用于缓存和页面的内存页将对整个系统性能产生重大影响。

If I were going to use a heuristic, I'd say that you should take the value of "Inactive" which is "The total amount of buffer or page cache memory, in kilobytes, that are free and available. This is memory that has not been recently used and can be reclaimed for other purposes." If you find that you take that and the system continues to work fine, you could estimate some percentage of "Active" to take as a guess as well because the system might have used some pages recently but isn't going to use them again. You know more about the system than me. If the system is dedicated to whatever you're about to do, then the pages and cached files in Active that may be used soon would depend on whether the system was recently used for something else.

如果我要用启发式,我会说你应该取“非活动”的值,即“缓冲或页缓存内存的总量,以千字节为单位,是*可用的”。这是最近没有使用的内存,可以用于其他用途。如果您发现您使用了它,并且系统继续正常工作,您可以估计“Active”的百分比作为猜测,因为系统可能最近使用了一些页面,但是不会再使用它们。你比我更了解这个系统。如果系统专门用于您将要做的任何事情,那么可能很快使用的活动中的页面和缓存文件将取决于系统最近是否用于其他事情。

#4


2  

I use the following:

我使用下面的:

FREE_KB = MemFree + Buffers + Cached

FREE_KB=$(($(echo `sed -n '2p;3p;4p' <  /proc/meminfo | sed "s/ \+/ /g" | cut -d' ' -f 2 ` | sed "s/ /+/g")))


USED_KB = MemTotal - MemFree - Buffers - Cached

USED_KB=$(($(echo `head -n4 /proc/meminfo | sed "s/ \+/ /g" | cut -d' ' -f 2 ` | sed "s/ /-/g")))

#5


2  

From linux-3.14 there is new metric MemAvailable in /proc/meminfo.

从linux-3.14中可以在/proc/ meminfo中找到新的memmetric。

And check line '-/+ buffers/cache:' in output of utility free.

并检查行'-/+缓冲区/缓存:'在无实用程序输出。