简体   繁体   中英

Tracking relative memory usage in c++17

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 (Ie, 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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM