Search code examples
c++resourcesmeasurement

Memory measurement in C++


Is it possible to measure amount of memory being released after the object being terminated by destructor. I'm trying to check whether resources are properly managed. For example, I've written an implementation of the LinkedList and testing it:

    int main(int argc, char** argv) {

//check point for initial memory 
int64_t init = ??? ;

//next points of measurement 
int64_t point_a, point_b;

List<int> list;
list.push_back(5);

{
    List<double> l;
    l.push_back(-0.12);
    l.push_back(1.6);
//  ... do something else 
//  ................
//  ................
    l.push_back(-4.75);
    l.push_back(7.8);
    l.print();

    point_a = ??? ;// memory state after operations with list
}//calling destructor 

point_b = ??? ; // memory state after destruction of the l - object 
std::cout << "Initial memory: " << init
          << ", memory in scope: " << point_b
          << ", after destructor: " << (point_b - point_a) << "\n";
    return 0;
}//main();

My questions are:

  1. Is it possible to achieve this ?
  2. If yes, what should I place instead of ??? ?
  3. Is there a way of doing better/differently ?
  4. Does it make any sense to do this at all ?

Solution

  • Is it possible to achieve this? Yes

    If yes, what should I place instead of ??? ? Use one of the tools described below.

    Is there a way of doing better/differently ? Yes, see Valgrind and MemWatch described below.

    Does it make any sense to do this at all ? No, if your code has errors like store gobbling, it is likely that your checks may have errors as well. Best to use a standard tool if you can, or for big projects, create a memory audit process that is simple and looks for leaks, logs it, and restarts any processes that consume too much memory.

    Two great tools for detecting memory leaks are Valgrind and MemWatch.

    To use valgrind to check your code for errors, if you run your program using:

    myprog arg1 arg2
    

    Then check for memory leaks using valgrind with the --leak-check option to turn on the detailed memory leak detector:

    valgrind --leak-check=yes myprog arg1 arg2
    

    Valgrind is great since you can run it on your compiled program as is.

    MemWatch is another useful tool but less popular. You can read about it here. It needs to be built into your program at compile time by including "memwatch.h" as your last include in each file.

    If -DMEMWATCH is defined at compile time, MemWatch replaces system calls that allocate/free memory with its own wrappers so that it can track memory leaks. When you run your code, it will produce a detailed log of all your allocations and report errors were memory was not freed properly. It is really good for students doing class assignments where the instructor asks the students to include it in their projects. Students and TAs can run these checks automatically without needing to learn how to interpret valgrind output.

    To build your project with MemWatch, download memwatch.c and memwatch.h here from GitHub, and compile using these options:

    gcc -o myProg -DMEMWATCH -DMEMWATCH_STDIO MyProg.c memwatch.c
    

    When you run your program with MemWatch enabled, MemWatch will report all the places where memory is mismanaged, and writes it to a log so you can view it after your code runs. If you do not define MEMWATCH and MEMWATCH_STDIO when compiling, MemWatch is not included in your compiled file.