0

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

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

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

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
  • Short answer yes. In windows you can use task manager and find the task that you have running. A super hacky way would be to load up all of your memory in a worst case and call `cin` then you can view the memory consumption. In linux I believe you can use `ps` command. As to how you would capture this information in a Dynamic fashion you could try `perf` maybe? – Matt Jul 23 '15 at 00:12
  • @Matt I could not get it. I need a little bit broad answer. – Enamul Hassan Jul 23 '15 at 00:13
  • Taking addresses of any local variable is a premature pessimization. It doesn't tell you much. `x` would most likely exist only in registers, had you not taken its address. The address of `&(*it)` is the address of a location within the vector's data. I don't quite understand what else were you expecting - the semantics of `std::vector` are such that data must be contiguous. Once you have the addresses of any two items, you have them all. – Kuba hasn't forgotten Monica Jul 23 '15 at 00:17
  • Your code is basically useless, it seems you have introduced it for no purpose whatsoever. It has nothing to do with your question at all. – Kuba hasn't forgotten Monica Jul 23 '15 at 00:18
  • @manetsus I mean like run the program to the point where you've consumed as much memory as you think you'll possibly consume and then have the program call `cin` so it pauses execution. Then you can use Windows/Linux/OSX to view the amount of memory that your process is consuming. – Matt Jul 23 '15 at 00:18
  • @KubaOber: Definitely you are right. My code is just an example. Any other code would work. – Enamul Hassan Jul 23 '15 at 00:25
  • @KubaOber I mention it in latest edit, is it okay now? – Enamul Hassan Jul 23 '15 at 00:40

1 Answers1

5

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

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    There are memory profiling tools builtin to Visual Sutdio as far as I know – sehe Jul 23 '15 at 00:15
  • This is a better solution than task manager haha. Thanks for this @sehe. – Matt Jul 23 '15 at 00:25
  • @sehe unfortunately, I could not get what is profiling – Enamul Hassan Jul 23 '15 at 00:26
  • 1
    https://en.wikipedia.org/wiki/Profiling_(information_science) se [What's the best free profiler for Windows](http://stackoverflow.com/questions/67554/whats-the-best-free-c-profiler-for-windows) e.g. – sehe Jul 23 '15 at 00:27