如何确定Linux中可用的物理内存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5284775/
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-05 03:14:29  来源:igfitidea点击:

how to determine the available physical memory in linux

linuxmemory

提问by eli

I'm trying to figure if my software running on linux suffers from memory leak. I've tried to measure the available physical memory as found in /proc/meminfo(see below) but could understand which field(s) represents the available memory and what is the relation between MemFree, Cached, Buffers, Active, Inactive.

我试图确定我的软件是否在linux suffers from memory leak. 我试图测量可用的物理内存/proc/meminfo(见下文),但可以理解哪些字段代表可用内存以及MemFree, Cached, Buffers, Active, Inactive.

cat /proc/meminfo
MemTotal:       124128 kB
MemFree:         62872 kB
Buffers:             0 kB
Cached:          15624 kB
SwapCached:          0 kB
Active:          38724 kB
Inactive:        11148 kB
SwapTotal:           0 kB
SwapFree:            0 kB
Dirty:               0 kB
Writeback:           0 kB
AnonPages:       34272 kB
Mapped:          14640 kB
Slab:             5564 kB
SReclaimable:      424 kB
SUnreclaim:       5140 kB
PageTables:        504 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
WritebackTmp:        0 kB
CommitLimit:     62064 kB
Committed_AS:    57936 kB
VmallocTotal:   655360 kB
VmallocUsed:      1016 kB
VmallocChunk:   654328 kB 

回答by Erik

If you're looking for memory leaks, use Valgrind.

如果您正在寻找内存泄漏,请使用Valgrind

For a quick check of your application's memory use, use getrusage()(requires a recent linux kernel) and look at the ru_maxrssvalue. /proc/meminfogives information about the system as a whole.

要快速检查应用程序的内存使用情况,请使用getrusage()(需要最新的 linux 内核)并查看ru_maxrss值。/proc/meminfo提供有关整个系统的信息。

回答by jesup

If you're looking to see if your software has a memory leak, look at either 'ps' or 'top' to look at your program. See if the virtual size (VSS) increases over time.

如果您想查看您的软件是否存在内存泄漏,请查看“ps”或“top”以查看您的程序。查看虚拟大小 (VSS) 是否随时间增加。

To debug such memory issues, use Valgrind or (my personal favorite) dmalloc.

要调试此类内存问题,请使用 Valgrind 或(我个人最喜欢的)dmalloc。

回答by ngduc

This is simpler command to check memory usage:

这是检查内存使用情况的更简单命令:

free

回答by freiheit

/proc/meminfo is for overall system memory information. /proc/[pid]/status has the memory usage info for an individual process. (it's also in /proc/[pid]/stat in a more machine parseable format).

/proc/meminfo 用于整体系统内存信息。/proc/[pid]/status 具有单个进程的内存使用信息。(它也在 /proc/[pid]/stat 中以一种更机器可解析的格式)。

In particular, VmData (size of data segment) and VmStk (size of stack segments) are most likely of use to you. Or just use psor topinstead of trying to read the data directly yourself.

特别是 VmData(数据段的大小)和 VmStk(堆栈段的大小)最有可能对您有用。或者只是使用psortop而不是尝试自己直接读取数据。

The other numbers are likely to just be confusing, because the overall system memory usage is complicated by shared memory, various kinds of buffers, etc.

其他数字可能只是令人困惑,因为整个系统内存使用情况因共享内存、各种缓冲区等而变得复杂。

回答by andrewdotn

Your question is asking something different, but since this is the #2 Google hit for “linux physical memory”—

您的问题提出了一些不同的问题,但由于这是“Linux 物理内存”的 #2 谷歌热搜——

Newer kernel versions running on x86 have DirectMap4k, DirectMap2M, and potentially DirectMap4Mand DirectMap1Gfields at the end of /proc/meminfo. Adding them up and multiplying by 1024 seems to give the number of bytes of physical RAM.

x86上运行较新版本的内核有DirectMap4kDirectMap2M,并有可能DirectMap4MDirectMap1G领域,在年底 /proc/meminfo。将它们相加并乘以 1024 似乎给出了物理 RAM 的字节数。