Enamul Hassan
Enamul Hassan

Reputation: 5445

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

Note: The program is just an example, main questions are just after this.

Suppose, I have a C++ program 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):

I am using C++ in Code::Blocks 13.12 in Windows 8.1

Upvotes: 0

Views: 101

Answers (1)

sehe
sehe

Reputation: 393789

Use a memory profiler.

On linux, e.g. use valgrind --tool=massif.

Demo:

--------------------------------------------------------------------------------
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

enter image description here

Upvotes: 5

Related Questions