简体   繁体   English

如何知道是否有足够的内存可以在 Linux 机器上部署新应用程序?

[英]How to know whether enough memory is free to deploy a new application on a Linux machine?

I have got a Linux machine whose memory snapshot (according to /proc/meminfo ) is as follows:我有一台 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 it, which will use 3 JVM instances whose typical combined memory requirement will be close to 1 GB.这是一台 16 GB 的机器,我有一个 Java 应用程序要部署在它上面,它将使用 3 个 JVM 实例,其典型的组合内存需求将接近 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?还有哪些其他统计数据可以帮助我做出决定,我如何收集这些统计数据?

(It's probably a bit late for the OP, but this is asked quite often, so I'll give it a shot) (对于 OP 来说可能有点晚了,但是经常有人问这个问题,所以我会试一试)

free normally 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.人们在试图找出他们有多少空闲内存时倾向于查看Mem:行。 Unfortunately that line is quite misleading, because the Linux kernel tries to make optimal use of the available memory in (at least) these ways:不幸的是,该行非常具有误导性,因为 Linux 内核试图以(至少)以下方式优化使用可用内存:

  • It will cache data from the I/O subsystem (eg the disk), so that it will be readily available if needed.它将缓存来自 I/O 子系统(例如磁盘)的数据,以便在需要时随时可用。

  • 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.这往往有利于吞吐量而不是响应,所以有些人调整他们的内核来改变这种行为。

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.第一点是关于free的混淆源,因为Mem:行包括已用内存量中用于缓存的内存。 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.事实上,在任何已经启动一段时间的 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 .虽然它会在一定程度上影响 I/O 性能,但其他进程可以在不使用交换空间的情况下拥有更多内存。 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:这就是为什么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.在上面的示例中,根据Mem:行,有大约 57 MB 的可用内存。 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.但是,如果阅读第二行,实际上大约有 3.9 GB可以使用,而无需强制活动进程进行交换。 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.作为旁注,还有大约 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. Cached :从磁盘读取的文件的内存缓存。 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. SwapCached :曾经被换出,然后换回但仍在交换空间中的内存。 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

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. free显示的数字充其量只是估计值,因为如果您想深入了解,还有很多其他变量需要考虑。 For people who regularly perform memory usage optimization, this is almost a form of art.对于经常进行内存使用优化的人来说,这几乎是一种艺术形式。

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 free misses a major chunk of cached data on modern Linux systems.即使是free也会错过现代 Linux 系统上的一大块缓存数据。 From /proc/meminfo on my system:从我系统上的/proc/meminfo

SReclaimable:    2253576 kB

That's about 2GB of memory that is used by the system slab allocator for caching directory entries and such and it is reclaimable (ie it can be cleared and used by processes if necessary).这大约是 2GB 的内存,由系统Slab分配器用于缓存目录条目等,并且它是可回收的(即,如果需要,它可以被进程清除和使用)。 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.然而, free并不认为它是缓存内存,也不会在任何计算中输入它,因此它显示为已用内存。

The slabtop utility, if available, allows the system administrator to find out what the slab cache is used for.如果可用, slabtop实用程序允许系统管理员找出slab缓存的用途。

A way (for the root user only) to have free show the actual memory use of the system is the following:一种(仅适用于 root 用户)可以free显示系统实际内存使用情况的方法如下:

# 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 free finally shows what the running processes actually use in its -/+ buffers/cache: line.然后free最后显示正在运行的进程在其-/+ buffers/cache:行中实际使用的内容。 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.值得注意的是,即使在删除所有缓存数据之后,内核也很快再次开始缓存——在这种情况下,它已经在几秒钟内达到了近 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 用户执行才能拥有必要的权限。

free -m total used free shared buff/cache available

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何估计JVM是否有足够的可用内存用于特定数据结构? - How to estimate if the JVM has enough free memory for a particular data structure? jvm崩溃..没有足够的免费记忆? (1.6) - jvm crash.. not enough free memory? (1.6) 如何使用JAVA获取虚拟机的可用内存大小(RAM) - How to get the free memory size (ram) of a virtual machine using JAVA 我怎么知道一个类的实例是否已经存在于内存中? - How can I know whether an instance of a class already exists in memory? 在Java中调用本机代码时如何知道是否存在内存泄漏? - How to know whether it is a memory leak or not when calling native code in Java? 如何知道显式锁是否已成功提供内存可见性? - How to know whether an explicit lock has successfully provided memory visibility? 如何估计给定的任务是否有足够的内存在 Java 中运行 - How to estimate whether a given task would have enough memory to run in Java 如何知道Java方法是否正在使用新的JTA事务? - How to know whether a Java method is using new JTA transaction or not? Linux:无法启动:内存不足 - Linux :Can't start up: not enough memory 如何真正释放Linux中的大页面以供新进程使用? - How to really free hugepages in Linux for use by a new process?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM