mercredi 22 juillet 2015

Is there any way to know the total memory consuming by the current program?

Suppose, I have a C++ code like this:

#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;
}

The output is:

-------------------
0x28fed4
0x28fed4
0x28fed4
0x28fed4
0x28fed4
0x28fed4
-------------------
0x3b21a8 4
0x3b21ac 5
0x3b21b0 3
0x3b21b4 2
0x3b21b8 5
0x3b21bc 42

From the memory addresses and increasing values, it is clear that auto is using the variable x each time which is in a new memory.

Now, I want to know, is there any way to know (built-in function or something like that):

  • How much memory the program used from the beginning to end of its execution?
  • What is the maximum memory it used?
  • How much memory currently it is using?

Aucun commentaire:

Enregistrer un commentaire