Memory

核心中程序記憶體測量的更新速度/頻率如何?

  • December 12, 2016

在他對 Linux 上的 proc 文件系統更新頻率如何?, Jonathan Ben-Avraham說,/proc/.../statm當你閱讀它時,它是最新的,因為閱讀它會直接觸發核心回調。

那些核心回調讀取的數據呢?這是否總是正確的,或者在 malloc/新請求的記憶體和核心之間是否存在一些時間延遲,使得測量值可供使用/proc/.../statm

我正在嘗試找到一種方法來測量通過malloc或分配的對象的目前大小new

執行一個分配一些數據的小型測試程序,無論/proc/.../statm是呼叫還是呼叫sbrk()似乎都不會立即與程序分配的記憶體量相關。

有沒有辦法得到準確的資訊?

程序分配 64KB 和 128KB 塊

$ ./a.out
MALLOC TEST. Size = 131072
0 ALLOC: HEAP SIZE: 0   MEMORY USAGE (statm): 1083 201 173 2 0 341 0
1 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1083 211 182 2 0 341 0
2 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1116 215 185 2 0 374 0
3 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1149 216 185 2 0 407 0
4 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1182 217 185 2 0 440 0
5 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1215 218 185 2 0 473 0
6 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1248 219 185 2 0 506 0
7 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1281 220 185 2 0 539 0
8 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1314 221 185 2 0 572 0
9 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1347 222 185 2 0 605 0

$ ./a.out
MALLOC TEST. Size = 65536
0 ALLOC: HEAP SIZE: 0   MEMORY USAGE (statm): 1067 201 174 2 0 325 0
1 ALLOC: HEAP SIZE: 0   MEMORY USAGE (statm): 1067 211 182 2 0 325 0
2 ALLOC: HEAP SIZE: 0   MEMORY USAGE (statm): 1067 215 185 2 0 325 0
3 ALLOC: HEAP SIZE: 196608  MEMORY USAGE (statm): 1115 216 185 2 0 373 0
4 ALLOC: HEAP SIZE: 196608  MEMORY USAGE (statm): 1115 217 185 2 0 373 0
5 ALLOC: HEAP SIZE: 196608  MEMORY USAGE (statm): 1115 218 185 2 0 373 0
6 ALLOC: HEAP SIZE: 393216  MEMORY USAGE (statm): 1163 219 185 2 0 421 0
7 ALLOC: HEAP SIZE: 393216  MEMORY USAGE (statm): 1163 220 185 2 0 421 0
8 ALLOC: HEAP SIZE: 393216  MEMORY USAGE (statm): 1163 221 185 2 0 421 0
9 ALLOC: HEAP SIZE: 589824  MEMORY USAGE (statm): 1211 222 185 2 0 469 0

測試程序

class CfgProfileList
{
public:
   bool obtainSystemProfileList();
   void leakTest();
   void leakObjTest();
   std::set<std::string> mProfileList;
private:
   char dummy[1024 * 1024]; // use up some space
};

class ComUtil
{
public:
   static void printMemoryUsage();

private:
   static unsigned int mHeapOrigin;
};

/* static */
unsigned int ComUtil::mHeapOrigin = 0;

// Print current process memory utilization
/* static */ void
ComUtil::printMemoryUsage()
{
   unsigned int pHeap = (unsigned int)sbrk(0);
   if (mHeapOrigin == 0)
       mHeapOrigin = pHeap;

   printf("HEAP SIZE: %u\t", pHeap - mHeapOrigin);

   char fname[256], line[256];
   sprintf(fname, "/proc/%d/statm", getpid());

   FILE *pFile = fopen(fname, "r");
   if (!pFile)
       return;

   fgets(line, 255, pFile);
   fclose(pFile);
   printf("MEMORY USAGE (statm): %s", line);
}



void
CfgProfileList::leakTest()
{
   char *pointerList[50];
   int  n = 10;
   int  sleep = 1; 
   int  size  = 64 * 1024;

   printf("MALLOC TEST. Size = %d\n", size);

   for (int i = 0; i < n; i++)
   {
       pointerList[i] = (char *)malloc(size);
       printf("%d ALLOC: ", i);
       ComUtil::printMemoryUsage();
       usleep(sleep);
   }
}

int
main(int argc, char **argv)
{
   CfgProfileList pl;
   pl.leakTest();
}

您的痕跡對我來說似乎大部分都可以……(請記住,您的程式碼沒有測量一個初始堆。)

MALLOC TEST. Size = 131072

在這裡,您要分配 128KB 塊,因此分配器可能不會使用sbrk(), 但是mmap().

0 ALLOC: HEAP SIZE: 0   MEMORY USAGE (statm): 1083 201 173 2 0 341 0
1 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1083 211 182 2 0 341 0

這是一個奇怪的,你的堆增加但不是你的程序頁面。

2 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1116 215 185 2 0 374 0
3 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1149 216 185 2 0 407 0
4 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1182 217 185 2 0 440 0
5 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1215 218 185 2 0 473 0
6 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1248 219 185 2 0 506 0
7 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1281 220 185 2 0 539 0
8 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1314 221 185 2 0 572 0
9 ALLOC: HEAP SIZE: 135168  MEMORY USAGE (statm): 1347 222 185 2 0 605 0

在這裡,每次分配都會佔用額外的 33 頁,即 132KB,從堆中取出,所以一切正常。

MALLOC TEST. Size = 65536
0 ALLOC: HEAP SIZE: 0   MEMORY USAGE (statm): 1067 201 174 2 0 325 0
1 ALLOC: HEAP SIZE: 0   MEMORY USAGE (statm): 1067 211 182 2 0 325 0
2 ALLOC: HEAP SIZE: 0   MEMORY USAGE (statm): 1067 215 185 2 0 325 0

在這裡,您的分配適合初始堆。

3 ALLOC: HEAP SIZE: 196608  MEMORY USAGE (statm): 1115 216 185 2 0 373 0
4 ALLOC: HEAP SIZE: 196608  MEMORY USAGE (statm): 1115 217 185 2 0 373 0
5 ALLOC: HEAP SIZE: 196608  MEMORY USAGE (statm): 1115 218 185 2 0 373 0

堆增加了 192KB,與分配的頁面匹配,並適合三個分配。

6 ALLOC: HEAP SIZE: 393216  MEMORY USAGE (statm): 1163 219 185 2 0 421 0
7 ALLOC: HEAP SIZE: 393216  MEMORY USAGE (statm): 1163 220 185 2 0 421 0
8 ALLOC: HEAP SIZE: 393216  MEMORY USAGE (statm): 1163 221 185 2 0 421 0

又是一樣…

9 ALLOC: HEAP SIZE: 589824  MEMORY USAGE (statm): 1211 222 185 2 0 469 0

… 然後再次。

執行strace -e brk,mmap將有助於理解事物。sbrk()提供的資訊/proc是準確的,沒有延遲。

引用自:https://unix.stackexchange.com/questions/329758