Skip to main content
deleted 1420 characters in body
Source Link
Erel Segal-Halevi
  • 37.4k
  • 41
  • 130
  • 201

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

EDIT: 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.
}
added 1415 characters in body
Source Link
Erel Segal-Halevi
  • 37.4k
  • 41
  • 130
  • 201

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

EDIT: 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.
}
Source Link
Erel Segal-Halevi
  • 37.4k
  • 41
  • 130
  • 201

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