想知道linux机器上是否有足够的内存来部署新的应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3784974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:16:22  来源:igfitidea点击:

Want to know whether enough memory is free on a linux machine to deploy a new application

javalinuxunixmemory-management

提问by Manish

I have got a linux machine whose memory snapshot when I do /proc/meminfo is :

我有一台 linux 机器,当我执行 /proc/meminfo 时,它的内存快照是:

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

This is a 16 GB machine and I have a Java application to deploy on this which will have 3 jvm instances and combined typical memory requirement for them will be close to 1 GB.

这是一台 16 GB 的机器,我有一个 Java 应用程序要部署在它上面,它将有 3 个 jvm 实例,它们的组合典型内存要求将接近 1 GB。

I wanted to know will it be safe to deploy that application without affecting other applications currently running on that machine. By looking at the above memory snapshot can we find out?

我想知道在不影响当前在该机器上运行的其他应用程序的情况下部署该应用程序是否安全。通过查看上面的内存快照我们可以发现吗?

What other statistics will help me to decide that and how to collect those statistics?

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

采纳答案by thkala

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

(对于 OP 来说可能有点晚了,但是经常有人问这个问题,所以我会试一试)

freenormally shows something like this:

free通常显示如下内容:

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

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:

人们Mem:在试图找出他们有多少可用内存时往往会查看这条线。不幸的是,该行非常具有误导性,因为 Linux 内核试图以(至少)以下方式优化使用可用内存:

  • 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.

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

  • 它将主动将一段时间不活动的进程驱逐到交换空间,以支持为活动进程缓存数据。这往往有利于吞吐量而不是响应能力,因此有些人调整他们的内核来改变这种行为。

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.

第一点是关于 的混淆源free,因为该Mem:行包括已用内存量中用于缓存的内存。但是,出于性能原因,内核将尽可能多地缓存。事实上,在任何已经启动一段时间的 Linux 系统上,空闲内存往往接近于零——未使用的内存就是浪费的内存。

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.

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

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

这就是为什么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.

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

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 GBthat 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.

在上面的示例中,根据该Mem:行,有 ~57 MB 的可用内存。但是,如果阅读第二行,实际上大约有 3.9 GB可以使用,而无需强制活动进程进行交换。作为旁注,还有大约 760 MB 很少使用的数据已被换出,以便在主内存中腾出更多空间用于进程和缓存。

At roughly the same time, the contents of /proc/meminfo:

大致同时,内容如下/proc/meminfo

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

MemTotal: the available physical memory detected by the kernel.

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

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

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

Buffers: relatively temporary storage of raw disk blocks.

Buffers:相对临时存储原始磁盘块。

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

Cached:从磁盘读取的文件的内存缓存。它不包括 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).

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

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

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

MemFree + Buffers + Cached + SwapCached

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

是一个很好的起点 - 并且free显示在第二行中。

Naturally, memory management and the related statistics and measurements are more complicated than this. The numbers shown by freeare 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.

自然,内存管理和相关的统计和测量比这更复杂。显示的数字free充其量只是估计值,因为如果您想深入了解,还有许多其他变量需要考虑。对于经常进行内存使用优化的人来说,这几乎是一种艺术形式。

EDIT:

编辑:

A somewhat humorous link about this "issue":

关于这个“问题”的一个有点幽默的链接:

http://www.linuxatemyram.com/

http://www.linuxatemyram.com/

EDIT 2:

编辑2:

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

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

Even freemisses a major chunk of cached data on modern Linux systems. From /proc/meminfoon my system:

甚至free错过了现代 Linux 系统上的一大块缓存数据。从/proc/meminfo我的系统上:

SReclaimable:    2253576 kB

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

这是大约 2GB 的内存,系统平板分配器使用它来缓存目录条目等,并且它是可回收的(即,如果需要,它可以被进程清除和使用)。然而,free它不考虑缓存内存,也不在任何计算中输入它,因此它显示为已用内存。

The slabtoputility, if available, allows the system administrator to find out what the slabcache is used for.

slabtop实用程序(如果可用)允许系统管理员找出Slab缓存的用途。

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

free显示系统实际内存使用情况的一种方法(仅适用于 root 用户)如下:

# 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

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.

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

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).

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

Then freefinally 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.

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

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

最后一个命令再次启用交换空间 - 只有在第一个命令也被使用时才需要。

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

需要注意的是,这些命令应该由 root 用户执行以获得必要的权限。