宏观内存信息统计

2024-06-11 17:58
文章标签 统计 内存 信息 宏观

本文主要是介绍宏观内存信息统计,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

/proc/meminfo统计信息解释

linux下内存的统计信息的解释:

 

例如 "cat /proc/meminfo":

MemTotal:  1031016 kB 
MemFree:  13548 kB
MemShared:  0 kB
Buffers:  98064 kB
Cached:   692320 kB
SwapCached:  2244 kB
Active:   563112 kB
Inact_dirty:  309584 kB
Inact_clean:  79508 kB
Inact_target:  190440 kB
HighTotal:  130992 kB
HighFree:  1876 kB
LowTotal:  900024 kB
LowFree:  11672 kB
SwapTotal:  1052248 kB
SwapFree:  1043908 kB
Committed_AS:  332340 kB  

The information comes in the form of both high-level and low-level statistics. At the top you see a quick summary of the most common values people would like to look at. Below you find the individual values we will discuss. First we will discuss the high-level statistics.

High-Level Statistics

  • MemTotal: Total usable ram (i.e. physical ram minus a few reserved bits and the kernel binary code)
  • MemFree: Is sum of LowFree+HighFree (overall stat)
  • MemShared: 0; is here for compat reasons but always zero.
  • Buffers: Memory in buffer cache. mostly useless as metric nowadays
  • Cached: Memory in the pagecache (diskcache) minus SwapCache
  • SwapCache: Memory that once was swapped out, is swapped back in but still also is in the swapfile (if memory is needed it doesn't need to be swapped out AGAIN because it is already in the swapfile. This saves I/O)

Detailed Level Statistics
VM Statistics

VM splits the cache pages into "active" and "inactive" memory. The idea is that if you need memory and some cache needs to be sacrificed for that, you take it from inactive since that's expected to be not used. The vm checks what is used on a regular basis and moves stuff around.

When you use memory, the CPU sets a bit in the pagetable and the VM checks that bit occasionally, and based on that, it can move pages back to active. And within active there's an order of "longest ago not used" (roughly, it's a little more complex in reality). The longest-ago used ones can get moved to inactive. Inactive is split into two in the above kernel (2.4.18-24.8.0). Some have it three.

  • Active: Memory that has been used more recently and usually not reclaimed unless absolutely necessary.
  • Inact_dirty: Dirty means "might need writing to disk or swap." Takes more work to free. Examples might be files that have not been written to yet. They aren't written to memory too soon in order to keep the I/O down. For instance, if you're writing logs, it might be better to wait until you have a complete log ready before sending it to disk.
  • Inact_clean: Assumed to be easily freeable. The kernel will try to keep some clean stuff around always to have a bit of breathing room.
  • Inact_target: Just a goal metric the kernel uses for making sure there are enough inactive pages around. When exceeded, the kernel will not do work to move pages from active to inactive. A page can also get inactive in a few other ways, e.g. if you do a long sequential I/O, the kernel assumes you're not going to use that memory and makes it inactive preventively. So you can get more inactive pages than the target because the kernel marks some cache as "more likely to be never used" and lets it cheat in the "last used" order.

Memory Statistics

  • HighTotal: is the total amount of memory in the high region. Highmem is all memory above (approx) 860MB of physical RAM. Kernel uses indirect tricks to access the high memory region. Data cache can go in this memory region.
  • LowTotal: The total amount of non-highmem memory.
  • LowFree: The amount of free memory of the low memory region. This is the memory the kernel can address directly. All kernel datastructures need to go into low memory.
  • SwapTotal: Total amount of physical swap memory.
  • SwapFree: Total amount of swap memory free.
  • Committed_AS: An estimate of how much RAM you would need to make a 99.99% guarantee that there never is OOM (out of memory) for this workload. Normally the kernel will overcommit memory. That means, say you do a 1GB malloc, nothing happens, really. Only when you start USING that malloc memory you will get real memory on demand, and just as much as you use. So you sort of take a mortgage and hope the bank doesn't go bust. Other cases might include when you mmap a file that's shared only when you write to it and you get a private copy of that data. While it normally is shared between processes. The Committed_AS is a guesstimate of how much RAM/swap you would need worst-case.

 

 

/proc/meminfo文件分析

> cat /proc/meminfo    读出的内核信息进行解释,

下篇文章会简单对读出该信息的代码进行简单的分析。

MemTotal:       507480 kB
MemFree:         10800 kB
Buffers:         34728 kB
Cached:          98852 kB
SwapCached:        128 kB
Active:         304248 kB
Inactive:        46192 kB
HighTotal:           0 kB
HighFree:            0 kB
LowTotal:       507480 kB
LowFree:         10800 kB
SwapTotal:      979956 kB
SwapFree:       941296 kB
Dirty:              32 kB
Writeback:           0 kB
AnonPages:      216756 kB
Mapped:          77560 kB
Slab:            22952 kB
SReclaimable:    15512 kB
SUnreclaim:       7440 kB
PageTables:       2640 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
CommitLimit:   1233696 kB
Committed_AS:   828508 kB
VmallocTotal:   516088 kB
VmallocUsed:      5032 kB
VmallocChunk:   510580 kB



相应选项中文意思想各位高手已经知道,如何翻译有什么错误,请务必指出:

    MemTotal: 所有可用RAM大小 (即物理内存减去一些预留位和内核的二进制代码大小)

     MemFree: LowFree与HighFree的总和,被系统留着未使用的内存

     Buffers: 用来给文件做缓冲大小

      Cached: 被高速缓冲存储器(cache memory)用的内存的大小(等于 diskcache minus SwapCache ).

  SwapCached:被高速缓冲存储器(cache memory)用的交换空间的大小
             已经被交换出来的内存,但仍然被存放在swapfile中。用来在需要的时候很快的被替换而不需要再次打开I/O端口。

      Active: 在活跃使用中的缓冲或高速缓冲存储器页面文件的大小,除非非常必要否则不会被移作他用.

    Inactive: 在不经常使用中的缓冲或高速缓冲存储器页面文件的大小,可能被用于其他途径.

   HighTotal:
    HighFree: 该区域不是直接映射到内核空间。内核必须使用不同的手法使用该段内存。

    LowTotal:
     LowFree: 低位可以达到高位内存一样的作用,而且它还能够被内核用来记录一些自己的数据结构。Among many
              other things, it is where everything from the Slab is
              allocated.  Bad things happen when you're out of lowmem.
         
   SwapTotal: 交换空间的总大小

    SwapFree: 未被使用交换空间的大小

    Dirty: 等待被写回到磁盘的内存大小。
     
   Writeback: 正在被写回到磁盘的内存大小。
  
   AnonPages:未映射页的内存大小
  
   Mapped: 设备和文件等映射的大小。
  
   Slab: 内核数据结构缓存的大小,可以减少申请和释放内存带来的消耗。

   SReclaimable:可收回Slab的大小
  
   SUnreclaim:不可收回Slab的大小(SUnreclaim+SReclaimable=Slab)
  
   PageTables:管理内存分页页面的索引表的大小。
  
   NFS_Unstable:不稳定页表的大小
  
   Bounce:

 CommitLimit: Based on the overcommit ratio ('vm.overcommit_ratio'),
              this is the total amount of  memory currently available to
              be allocated on the system. This limit is only adhered to
              if strict overcommit accounting is enabled (mode 2 in
              'vm.overcommit_memory').
              The CommitLimit is calculated with the following formula:
              CommitLimit = ('vm.overcommit_ratio' * Physical RAM) + Swap
              For example, on a system with 1G of physical RAM and 7G
              of swap with a `vm.overcommit_ratio` of 30 it would
              yield a CommitLimit of 7.3G.
              For more details, see the memory overcommit documentation
              in vm/overcommit-accounting.
             
Committed_AS: The amount of memory presently allocated on the system.
              The committed memory is a sum of all of the memory which
              has been allocated by processes, even if it has not been
              "used" by them as of yet. A process which malloc()'s 1G
              of memory, but only touches 300M of it will only show up
              as using 300M of memory even if it has the address space
              allocated for the entire 1G. This 1G is memory which has
              been "committed" to by the VM and can be used at any time
              by the allocating application. With strict overcommit
              enabled on the system (mode 2 in 'vm.overcommit_memory'),
              allocations which would exceed the CommitLimit (detailed
              above) will not be permitted. This is useful if one needs
              to guarantee that processes will not fail due to lack of
              memory once that memory has been successfully allocated.

VmallocTotal: 可以vmalloc虚拟内存大小

VmallocUsed: 已经被使用的虚拟内存大小。

VmallocChunk: largest contigious block of vmalloc area which is free

linux中内存是如何使用的。
当有应用需要读写磁盘数据时,由系统把相关数据从磁盘读取到内存,如果物理内存不够,则把内存中的部分数据导入到磁盘,从而把磁盘的部分空间当作虚拟内存来使用,也称为Swap。如果给所有应用分配足够内存后,物理内存还有剩余,linux会尽量再利用这些空闲内存,以提高整体I/O效率,其方法是把这部分剩余内存再划分为cache及buffer两部分加以利用。
从磁盘读取到内存的数据在被相关应用程序读取后,如果有剩余内存,则这部分数据会存入cache,以备第2次读取时,避免重新读取磁盘。当一个应用程序在内存中修改过数据后,因为写入磁盘速度相对较低,在有空闲内存的情况下,这些数据先存入buffer,在以后某个时间再写入磁盘,从而应用程序可以继续后面的操作,而不必等待这些数据写入磁盘的操作完成。
如果在某个时刻,系统需要更多的内存,则会把cache部分擦除,并把buffer中的内容写入磁盘,从而把这两部分内存释放给系统使用,这样再次读取cache中的内容时,就需要重新从磁盘读取了。

通过以上分析可以得知,空闲物理内存不多,不一定表示系统运行状态很差,因为内存的cache及buffer部分可以随时被重用,在某种意义上,这两部分内存也可以看作诗额外的空闲内存。swap如果被频繁调用,bi,bo长时间不为0,则才是内存资源是否紧张的依据。



下面简单来个例子,看看已用内存和物理内存大小..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int MemInfo(char* Info, int len);
int main()
{
  char buf[128];

  memset(buf, 0, 128);
  MemInfo(buf, 100);
  printf("%s", buf);
  return 0;
}
int MemInfo(char* Info, int len)
{
  char sStatBuf[256];
  FILE* fp;
  int flag;
  int TotalMem;
  int UsedMem;
  char* line;
  if(system("free -m | awk '{print $2,$3}' > mem"));
  memset(sStatBuf, 0, 256);
  fp = fopen("mem", "rb");
  if(fp < 0)
  {
    return -1;
   }
  fread(sStatBuf,1, sizeof(sStatBuf) , fp);
 
  line = strstr(sStatBuf, "/n");
  TotalMem = atoi(line);
  line = strstr(line, " ");
  UsedMem = atoi(line);
  memset(sStatBuf, 0, 256);
  sprintf(sStatBuf, "Used %dM/Total %dM/n", UsedMem, TotalMem);
  if(strlen(sStatBuf) > len)
   {
     return -1;
   }
   memcpy(Info, sStatBuf, strlen(sStatBuf));
   return 0;
}


结果:Used 488M/Total 495M



这篇关于宏观内存信息统计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1051885

相关文章

NameNode内存生产配置

Hadoop2.x 系列,配置 NameNode 内存 NameNode 内存默认 2000m ,如果服务器内存 4G , NameNode 内存可以配置 3g 。在 hadoop-env.sh 文件中配置如下。 HADOOP_NAMENODE_OPTS=-Xmx3072m Hadoop3.x 系列,配置 Nam

hdu1496(用hash思想统计数目)

作为一个刚学hash的孩子,感觉这道题目很不错,灵活的运用的数组的下标。 解题步骤:如果用常规方法解,那么时间复杂度为O(n^4),肯定会超时,然后参考了网上的解题方法,将等式分成两个部分,a*x1^2+b*x2^2和c*x3^2+d*x4^2, 各自作为数组的下标,如果两部分相加为0,则满足等式; 代码如下: #include<iostream>#include<algorithm

业务中14个需要进行A/B测试的时刻[信息图]

在本指南中,我们将全面了解有关 A/B测试 的所有内容。 我们将介绍不同类型的A/B测试,如何有效地规划和启动测试,如何评估测试是否成功,您应该关注哪些指标,多年来我们发现的常见错误等等。 什么是A/B测试? A/B测试(有时称为“分割测试”)是一种实验类型,其中您创建两种或多种内容变体——如登录页面、电子邮件或广告——并将它们显示给不同的受众群体,以查看哪一种效果最好。 本质上,A/B测

【北交大信息所AI-Max2】使用方法

BJTU信息所集群AI_MAX2使用方法 使用的前提是预约到相应的算力卡,拥有登录权限的账号密码,一般为导师组共用一个。 有浏览器、ssh工具就可以。 1.新建集群Terminal 浏览器登陆10.126.62.75 (如果是1集群把75改成66) 交互式开发 执行器选Terminal 密码随便设一个(需记住) 工作空间:私有数据、全部文件 加速器选GeForce_RTX_2080_Ti

flume系列之:查看flume系统日志、查看统计flume日志类型、查看flume日志

遍历指定目录下多个文件查找指定内容 服务器系统日志会记录flume相关日志 cat /var/log/messages |grep -i oom 查找系统日志中关于flume的指定日志 import osdef search_string_in_files(directory, search_string):count = 0

hdu4267区间统计

题意:给一些数,有两种操作,一种是在[a,b] 区间内,对(i - a)% k == 0 的加value,另一种操作是询问某个位置的值。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import

hdu4417区间统计

给你一个数列{An},然后有m次查询,每次查询一段区间 [l,r] <= h 的值的个数。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamRead

hdu3333区间统计

题目大意:求一个区间内不重复数字的和,例如1 1 1 3,区间[1,4]的和为4。 import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;

实例:如何统计当前主机的连接状态和连接数

统计当前主机的连接状态和连接数 在 Linux 中,可使用 ss 命令来查看主机的网络连接状态。以下是统计当前主机连接状态和连接主机数量的具体操作。 1. 统计当前主机的连接状态 使用 ss 命令结合 grep、cut、sort 和 uniq 命令来统计当前主机的 TCP 连接状态。 ss -nta | grep -v '^State' | cut -d " " -f 1 | sort |

JVM内存调优原则及几种JVM内存调优方法

JVM内存调优原则及几种JVM内存调优方法 1、堆大小设置。 2、回收器选择。   1、在对JVM内存调优的时候不能只看操作系统级别Java进程所占用的内存,这个数值不能准确的反应堆内存的真实占用情况,因为GC过后这个值是不会变化的,因此内存调优的时候要更多地使用JDK提供的内存查看工具,比如JConsole和Java VisualVM。   2、对JVM内存的系统级的调优主要的目的是减少