有没有办法知道当前程序的总内存消耗?

Ena*_*san 0 c++ memory memory-management c++11

注意:该程序只是一个例子,主要问题就在此之后.

假设,我有一个C++这样的程序:

#include<iostream>
#include<vector>
using namespace std;

int main()
{
    vector<int>numbers = {4,5,3,2,5,42};
    cout<<"-------------------\n";
    for (auto x : numbers){
        cout<< &x <<endl;
        x+=10;
    }

    cout<<"-------------------\n";
    for (vector<int>::iterator it = numbers.begin(); it!=numbers.end(); it++){
        cout<< &(*it) <<" "<< *it << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是:

-------------------
0x28fed4
0x28fed4
0x28fed4
0x28fed4
0x28fed4
0x28fed4
-------------------
0x3b21a8 4
0x3b21ac 5
0x3b21b0 3
0x3b21b4 2
0x3b21b8 5
0x3b21bc 42
Run Code Online (Sandbox Code Playgroud)

从内存地址和增加的值,很明显每次auto使用变量x都在新的内存中.

现在,我想知道,有没有办法知道(内置函数或类似的东西):

  • 程序从执行开始到结束使用的内存量是多少?
  • 它使用的最大内存是多少?
  • 目前它使用了多少内存?

我在Windows 8.1中的Code :: Blocks 13.12中使用C++

seh*_*ehe 5

使用内存分析器.

在linux上,例如使用valgrind --tool=massif.

演示:

--------------------------------------------------------------------------------
Command:            ./test
Massif arguments:   (none)
ms_print arguments: massif.out.26621
--------------------------------------------------------------------------------


     B
   40^                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
     |                                                                      :#
   0 +----------------------------------------------------------------------->Mi
     0                                                                   1.397

Number of snapshots: 4
 Detailed snapshots: [2 (peak)]

--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
  0              0                0                0             0            0
  1      1,425,892               40               24            16            0
  2      1,464,762               40               24            16            0
60.00% (24B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->60.00% (24B) 0x400B2D: main (new_allocator.h:104)

--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
  3      1,464,762                0                0             0            0
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述