1

I am trying to understand the intricacies of memory management in C++, in order to teach it to my students. Since the best way to learn is by trying, I would like to try several code snippets and see how they affect the memory usage. For example, I would like to try the following in order to understand how unique pointers work:

#include <memory>
using namespace std;

int main() 
{
    {
        // print memory - should be X
        auto i = make_unique<int>();
        // print memory - should be X-4
    }
    // print memory - should be X
}

The comments I wrote are based on my current understanding, which of course might be wrong; I want to check whether I understood correctly. My question is: what can I write instead of "print memory"?

I found several apparently similar questions, such as: How to determine CPU and memory consumption from inside a process? and C++: Measuring memory usage from within the program, Windows and Linux . However, the answers there are very complicated and platform-dependent. My need is much simpler: I do not need the absolute memory consumption of my program (I.e, I do not need X to be accurate). All I need is a relative measurement that will show me how my actions affect the memory consumption. For this need, is there a simpler solution?

9
  • 2
    you can write your own allocator / overload new Commented Jan 23, 2018 at 6:44
  • @appleapple That won't handle memory allocated on the stack. Commented Jan 23, 2018 at 6:50
  • ... I guess you can wrap the "complex" methods into a method/library. Commented Jan 23, 2018 at 6:51
  • @user202729 right, but based on what OP write, I think he is more interest in dynamic memory. BTW, for stack memory, usually address of objects is sufficient. Commented Jan 23, 2018 at 6:53
  • 1
    Why don't you just run it under memory profiler? Commented Jan 23, 2018 at 7:36

2 Answers 2

3

Couldn't you make the unique_ptr hold a structure way larger than an integer, keeping Kbs instead of bytes? Then maybe just inspecting the process memory in the task manager (or whatever your OS uses) you could show that to your students going step by step through the code.

Sign up to request clarification or add additional context in comments.

Comments

1

Following the answer of Jorge Y., I created the following program. It is not optimal since (a) it works only on Linux, (b) it requires me to keep a terminal window open besides the program console to track the memory. But, at least it is good for demonstration purposes.

#include <iostream>
#include <memory>
#include <vector>
#include <thread>
#include <chrono>
using namespace std;

#define USE_UNIQUE_PTR

constexpr int SIZE=1000*1000*1000;
struct Large {
    char x[SIZE];
};

int main() {
    cout << "Before braces" << endl;
    this_thread::sleep_for(chrono::milliseconds(5000));
    // On Linux, run:    cat /proc/meminfo |grep MemFree
    // Available memory is X
    {
        #ifdef USE_UNIQUE_PTR
        auto p = make_unique<Large>();
        #else
        auto p = new Large();
        #endif
        cout << "Inside braces" << endl;
        p->x[0] = 5;
        cout << p->x[0] << endl;
        this_thread::sleep_for(chrono::milliseconds(5000));
        // On Linux, run:    cat /proc/meminfo |grep MemFree
        // Available memory should be X-SIZE
    }
    cout << "Outside braces" << endl;
    this_thread::sleep_for(chrono::milliseconds(5000));
    // On Linux, run:    cat /proc/meminfo |grep MemFree
    // Available memory should be X if USE_UNIQUE_PTR is defined, X-SIZE if it is undefined.
}

1 Comment

I'm glad the workaround worked for you. In Windows you could use the task manager for the same purpose. In the task manager you have the "Processes" tab where one of the available columns is the memory used by the process. That should show the memory changes happening in your program.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.