Linux 总可用内存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8348797/
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
Linux total available memory
提问by atomicbaum
I'm trying to figure out a good formula for finding out how much memory is available. I'm using the following formula currently: freeMem = MemFree + Buffers + Cached - Shmem
. However, according to this formula my embedded system is losing memory. Now I'm wondering if I have a memory leak so I enabled kmemleak
in the kernel. According to mpatrol
, valgrind
, and coverity
I do not have any leaks in user space. Is there a leak in kernel space or is my formula off? Note that I do not have any swap for this device.
我试图找出一个很好的公式来找出有多少内存可用。我目前使用以下公式:freeMem = MemFree + Buffers + Cached - Shmem
. 但是,根据这个公式,我的嵌入式系统正在丢失内存。现在我想知道我是否有内存泄漏,所以我kmemleak
在内核中启用。根据mpatrol
, valgrind
, 和coverity
I 在用户空间中没有任何泄漏。内核空间是否存在泄漏或者我的公式是否关闭?请注意,我没有此设备的任何交换。
MYBOX> cat /proc/meminfo
MemTotal: 2073348 kB
MemFree: 1388180 kB
Buffers: 137016 kB
Cached: 88772 kB
SwapCached: 0 kB
Active: 589124 kB
Inactive: 44380 kB
Active(anon): 410236 kB
Inactive(anon): 1992 kB
Active(file): 178888 kB
Inactive(file): 42388 kB
Unevictable: 0 kB
Mlocked: 0 kB
HighTotal: 1310716 kB
HighFree: 811828 kB
LowTotal: 762632 kB
LowFree: 576352 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 64 kB
Writeback: 0 kB
AnonPages: 407712 kB
Mapped: 26140 kB
Shmem: 4516 kB
Slab: 40408 kB
SReclaimable: 8320 kB
SUnreclaim: 32088 kB
KernelStack: 1480 kB
PageTables: 1464 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 1036672 kB
Committed_AS: 660508 kB
VmallocTotal: 237344 kB
VmallocUsed: 104556 kB
VmallocChunk: 126296 kB
回答by Quentin Casasnovas
Leaking memory from userland would not show anyway in /proc/meminfo
because as far as the kernel is concerned it is memory allocated (no matter if you use free() in your userland app or not, it's either allocated with the mmap() syscall or brk()/sbrk() and the kernel keeps track of the pages allocated in userland, otherwise we would be in serious trouble ;).
从用户态泄漏的内存无论如何都不会显示,/proc/meminfo
因为就内核而言,它是内存分配的(无论您是否在用户态应用程序中使用 free(),它要么使用 mmap() 系统调用或 brk() 分配/sbrk() 并且内核会跟踪用户态中分配的页面,否则我们会遇到严重的麻烦;)。
I don't clearly understand how you came up with your belief that your are leaking memory? Here is a good link redhat/meminfoif you haven't read it already which explains what each statistic really means.
我不太明白你是如何得出你的内存泄漏的信念的?如果您还没有阅读它,这里有一个很好的链接redhat/meminfo,它解释了每个统计数据的真正含义。
回答by er0
Agreed with auxv - using /proc/meminfo is probably not the best way of tracking user process memory, as it includes memory allocated by all user processes making it difficult to narrow down the consumption of your process.
同意 auxv - 使用 /proc/meminfo 可能不是跟踪用户进程内存的最佳方式,因为它包括所有用户进程分配的内存,因此很难缩小进程的消耗范围。
A better way to track the total memory consumed by your process would be to use top (1)
and look at VIRT (which includes memory swapped out) or RES (which only includes physical memory).
跟踪进程消耗的总内存的更好方法是使用top (1)
并查看 VIRT(包括换出的内存)或 RES(仅包括物理内存)。
But if you do want to use /proc/meminfo then the formula I would use would be:
但是如果你确实想使用 /proc/meminfo 那么我会使用的公式是:
MemTotal = MemFree + Cached + Buffers + SwapCached
MemTotal = MemFree + Cached + Buffers + SwapCached
...note that this only accounts for data, not code. Most of MemTotal - (quantity on the right of the equation) should be your kernel image.
...请注意,这仅说明数据,而不是代码。MemTotal 的大部分 -(等式右侧的数量)应该是您的内核映像。
回答by caf
Your "free memory" calculation is missing one thing - it should be adding SReclaimable
(for the reclaimable slab cache) to the effectively-free memory.
您的“可用内存”计算缺少一件事 - 它应该添加SReclaimable
(对于可回收的平板缓存)到有效可用的内存中。
If this does not change the slow reduction in effectively-free memory over time, you should take snapshots of /proc/meminfo
at regular intervals and identify which line shows the increase.
如果这不会改变有效可用内存随时间缓慢减少的情况,您应该/proc/meminfo
定期拍摄快照并确定哪一行显示增加。
If it is the SUnreclaim
line that is increasing, you can look in /proc/slabinfo
to see your kernel slab usage and identify the culprit. It is possible that it is simply memory fragmentation that you are observing, and it will eventually settle down over a longer time period.
如果SUnreclaim
是增加的行,您可以查看/proc/slabinfo
内核平板使用情况并确定罪魁祸首。可能只是您观察到的内存碎片,它最终会在更长的时间内稳定下来。
回答by user1403360
For my systems I am using the following command to check how much memory is consumed:
对于我的系统,我使用以下命令来检查消耗了多少内存:
ps aux | awk '{sum +=}'
This adds the total percentage of used memory for all processes that are currently running.
这增加了当前正在运行的所有进程的已用内存的总百分比。
回答by Samuel
FreeMem = MemFree + Buffers + Cached - Mapped, The cached memory contains the mapped part, this part have been mapped to user space.
FreeMem = MemFree + Buffers + Cached - Mapped,缓存的内存包含映射的部分,这部分已经映射到用户空间。
回答by Arjun Jv
Previously it was MemFree + catched ( which is correct many years back).
以前它是 MemFree + 捕获(多年前这是正确的)。
Now it is MemFree + Active(file) + Inactive(file) + SRreclaimable .
现在是 MemFree + Active(file) + Inactive(file) + SRreclaimable 。
For further reference go trough the link below by LINUX TORVALD.
如需进一步参考,请通过 LINUX TORVALD 访问以下链接。