本文介绍了如何知道是否有足够的可用内存在Linux机器上部署新应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一台Linux机器,其内存快照(根据/proc/meminfo )如下:

I have got a Linux machine whose memory snapshot (according to /proc/meminfo) is as follows:

MemTotal:     16413388 kB
MemFree:         48296 kB
Buffers:        193600 kB
Cached:        1986448 kB
SwapCached:     874512 kB
Active:       15034264 kB
Inactive:       713672 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:     16413388 kB
LowFree:         48296 kB
SwapTotal:     8385920 kB
SwapFree:      4682408 kB
Dirty:            3124 kB
Writeback:           0 kB
Mapped:       13005560 kB
Slab:           257784 kB
CommitLimit:  16592612 kB
Committed_AS: 59624324 kB
PageTables:     233748 kB
VmallocTotal: 536870911 kB
VmallocUsed:    267064 kB
VmallocChunk: 536603555 kB
HugePages_Total:     0
HugePages_Free:      0
Hugepagesize:     2048 kB

这是一台16 GB的计算机,我要在上面部署一个Java应用程序,它将使用3个JVM实例,它们的典型组合内存需求将接近1 GB.

This is a 16 GB-machine, and I have a Java application to deploy on it, which will use 3 JVM instances whose typical combined memory requirement will be close to 1 GB.

如何确保在不影响当前在该计算机上运行的其他应用程序的情况下安全地部署该应用程序.是否可以从上面的内存快照中找到答案?

How can I make sure that it will be safe to deploy the said application without affecting other applications currently running on that machine. Is it possible to find that out from the above memory snapshot?

还有哪些其他统计信息可以帮助我做出决定,以及如何收集这些统计信息?

What other statistics may help me to decide that, and how can I collect those statistics?

推荐答案

(对于OP来说可能有点晚了,但这是经常问到的,所以我会给它一个机会)

(It's probably a bit late for the OP, but this is asked quite often, so I'll give it a shot)

免费通常显示如下内容:

             total       used       free     shared    buffers     cached
Mem:       8195284    8137708      57576          0    1232328    2651156
-/+ buffers/cache:    4254224    3941060
Swap:     18892216     759852   18132364

人们在试图找出他们有多少可用内存时倾向于查看 Mem:行.不幸的是,这句话很容易引起误解,因为Linux内核试图通过(至少)以下方式来最佳利用可用内存:

People tend to look at the Mem: line when trying to find out how much free memory they have. Unfortunately that line is quite misleading, because the Linux kernel tries to make optimal use of the available memory in (at least) these ways:

  • 它将缓存来自I/O子系统(例如磁盘)的数据,以便在需要时可以随时使用.

  • It will cache data from the I/O subsystem (e.g. the disk), so that it will be readily available if needed.

它将主动将在一段时间内处于非活动状态的进程逐出交换空间,以便为活动进程缓存数据.相比于响应速度,这往往更倾向于吞吐量,因此有些人调整了内核以更改此行为.

It will actively evict processes that have been inactive for some time to the swap space, in favour of caching data for active processes. This tends to favour throughput over responsiveness, so some people tune their kernel to change this behaviour.

第一点是关于 free 的困惑之源,因为 Mem:行包括用于缓存已用内存量的内存.但是,出于性能原因,内核将尽可能多地缓存.实际上,在已经运行了一段时间的任何Linux系统上,可用内存都趋于接近于零-未使用的内存是浪费的内存.

The first point is the source of confusion regarding free, because the Mem: line includes the memory used for caching in the used memory amount. The kernel, though, will cache as much as possible for performance reasons. In fact, on any Linux system that has been up for some time, the free memory tends to be close to zero - unused memory is wasted memory.

不过,如果另一个进程需要,则内核可以释放高速缓存.虽然它会在一定程度上影响I/O性能,但其他进程可以在不使用交换空间的情况下拥有更多的内存 .因此,出于大多数意图和目的,该内存是免费的.

The cache memory, though, can be freed by the kernel if needed by another process. While it will impact I/O performance to a degree, other processes can have more memory without using the swap space. Therefore, for most intents and purposes, that memory is free.

这就是为什么 free 包含第二行的原因,在第二行中,缓存被认为是空闲的:

That's why free includes a second line, where the cache memory is considered free:

-/+ buffers/cache:    4254224    3941060

第二行是人们在想知道他们是否有足够的空闲内存用于特定目的时应该查看的内容.

This second line is what people should be looking at when they want to know if they have enough free memory for a certain purpose.

在上面的示例中,根据 Mem:行,大约有57 MB的可用内存.但是,如果读取第二行,则实际上可以使用大约3.9 GB ,而无需强制活动进程进行交换.附带说明一下,还交换了约760 MB的很少使用的数据,以在主内存中留出更多空间用于处理和缓存.

In the example above, according to the Mem: line there are ~57 MB of free memory. If one reads the second line, though, there are in fact about 3.9 GB that can be used without forcing active processes to swap. As a sidenote, there are also about 760 MB of rarely-used data that have been swapped out, to make more space in the main memory for processes and caching.

大致同时,/proc/meminfo 的内容:

MemTotal:        8195284 kB
MemFree:           57660 kB
Buffers:         1232352 kB
Cached:          2651156 kB
SwapCached:       119936 kB
.
.
.

MemTotal :内核检测到的可用物理内存.

MemTotal: the available physical memory detected by the kernel.

MemFree :未使用的物理内存-在 free Mem:行中显示的可用内存.

MemFree: the unused physical memory - the free memory shown in the Mem: line of free.

缓冲区:相对临时存储原始磁盘块.

Buffers: relatively temporary storage of raw disk blocks.

已缓存:用于从磁盘读取的文件的内存中缓存.它不包含SwapCached内存.

Cached: in-memory cache for files read from the disk. It does not include SwapCached memory.

SwapCached :曾经被换出然后又换回但仍在交换空间中的内存.如果需要,可以将其内容丢弃(非常快!),而不必将它们换出(更慢).

SwapCached: memory that was once swapped out, then swapped back in but is still in the swap space. If needed, its contents can be just discarded (very fast!), without having to swap them out (slower).

因此,要对实际可用的内存进行半准确的估算

So, to have a semi-accurate estimate of the memory that is actually available

MemFree + Buffers + Cached + SwapCached

是一个很好的起点-第二行显示一个 free .

is a good starting point - and the one free shows in that second line.

自然地,内存管理以及相关的统计和测量要比这复杂得多. free 所显示的数字充其量仅是估计值,因为如果您想更深入一点,还需要考虑很多其他变量.对于经常执行内存使用优化的人们来说,这几乎是一种艺术.

Naturally, memory management and the related statistics and measurements are more complicated than this. The numbers shown by free are mere estimates at best, since there are a lot of other variables to take into account if you want to go deeper. For people who regularly perform memory usage optimization, this is almost a form of art.

有关此问题"的幽默链接:

A somewhat humorous link about this "issue":

http://www.linuxatemyram.com/

要确认有关内存使用分析的评论几乎是一种艺术形式:

To confirm the comment about memory use analysis almost being a form of art:

即使 free ,在现代Linux系统上也会丢失大部分的缓存数据.从我系统上的/proc/meminfo :

Even free misses a major chunk of cached data on modern Linux systems. From /proc/meminfo on my system:

SReclaimable:    2253576 kB

大约2GB的内存,系统 slab 分配器用于缓存目录条目和这样,它是可回收的(即,可以在必要时将其清除并由进程使用).但是 free 不会将其视为缓存内存,也不会将其输入任何计算中,因此会显示为已用内存.

That's about 2GB of memory that is used by the system slab allocator for caching directory entries and such and it is reclaimable (i.e. it can be cleared and used by processes if necessary). Yet free does not consider it cache memory and does not enter it in any of its calculations and therefore it shows up as used memory.

slabtop 实用程序(如果可用)允许系统管理员找出 slab 缓存用于.

The slabtop utility, if available, allows the system administrator to find out what the slab cache is used for.

以下一种(仅对于root用户而言)免费显示系统实际内存使用情况的方法是

A way (for the root user only) to have free show the actual memory use of the system is the following:

# swapoff -a
# sync
# echo 3 > /proc/sys/vm/drop_caches
# free
             total       used       free     shared    buffers     cached
Mem:       8195284    3181468    5013816          0       8656     228832
-/+ buffers/cache:    2943980    5251304
Swap:            0          0          0
# swapon -a

第一个命令禁用交换空间.如果可用内存可能不足以容纳已换出的数据,则不应发出该消息-在这种情况下,必须在计算内存使用量时考虑 Swap:空闲行

The first command disables the swap space. It should not be issued if the available memory may not be enough to hold the data that have been swapped out - in that case one has to take into account the Swap: line of free in their memory usage calculations.

第二条命令将所有缓冲的数据推入磁盘.下一步,它可以释放更多的缓存.

The second command pushes all buffered data to the disk. It allows more cache memory to be freed in the next step.

第三个命令是集合中最重要的命令-它强制内核丢弃尽可能多的缓存数据(页面缓存,目录条目,索引节点等).

The third command is the most important of the set - it forces the kernel to discard as much cached data as possible (page cache, directory entries, inodes etc).

然后 free 最终在其-/+ buffers/cache:行中显示正在运行的进程实际使用的内容.值得注意的是,即使在删除所有缓存的数据之后,内核仍会迅速再次开始缓存-在这种情况下,它已经在几秒钟内达到了将近250MB的缓存数据.

Then free finally shows what the running processes actually use in its -/+ buffers/cache: line. It is quite noticeable that even after dropping all cached data the kernel quickly starts caching again - in this case it has already reached almost 250MB of cached data within a few seconds.

最终命令再次启用交换空间-仅在也使用了第一个命令的情况下才有必要.

The final command enables the swap space again - it is only necessary if the first command was used too.

应注意,这些命令应由root用户执行,以便具有必要的特权.

It should be noted that these commands should be executed by the root user in order to have the necessary privileges.

这篇关于如何知道是否有足够的可用内存在Linux机器上部署新应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 07:29