1

I have to compare memory usage of two different implementations of the same alogrithm experimentally. What is the right way to do this? I have implemented both the versions in C++, and would like to do it from the command line in linux.

But, I am confused about which is the best way to do it accurately ?

Mona
  • 19
  • 1
  • [How to measure actual memory usage of an application or process?](http://stackoverflow.com/questions/131303/how-to-measure-actual-memory-usage-of-an-application-or-process) – Arash Mar 14 '17 at 18:46
  • A simple way using the command terminal would be to use the command "htop", this will show you the processes you have running on your computer, then you go to the name of the programs and look at the amount of ram used. – Roger Figueroa Quintero Mar 14 '17 at 19:23

2 Answers2

2

You can implement the global new and delete operators like this:

#include <stdlib.h>

static size_t curUsage = 0, maxUsage = 0;
void* operator new (size_t size) {
    curUsage += size;
    if(maxUsage < curUsage) maxUsage = curUsage;

    size_t* result = (size_t*)malloc(size + sizeof(size_t));
    *result = size;
    return result + 1;
}
void operator delete (void *pointer) {
    size_t* originalPtr = (size_t*)pointer - 1;
    curUsage -= *originalPtr;
    free(originalPtr);
}

Assuming you are using gcc, you can then output the max memory usage at the end of the run by simply adding this function:

#include <stdio.h>
__attribute__ ((destructor)) void printMaxUsage() {
    printf("max memory usage: %jd bytes\n", maxUsage);
}

This will catch any allocation made with new and delete. However, it won't account for used stack space. If you need to take that into account (due to deep recursion and/or large local variables), you must use a different approach. However, for well behaved code, the above should be good enough.

Note that you don't need any changes to any other files, you just have to link in these three functions, and you get the output.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
  • The only caveat is that this won't measure the stack, so you'll have difference depending on how many local variables you have (which can be easily estimated manually), or if your algorithm is recursive. – cbuchart Mar 15 '17 at 14:15
  • 1
    @cbuchart True. It won't catch allocations made by `malloc()` either. Nevertheless, I've added a note on the stack space issue now as stack space usage is more common in C++ applications than `malloc()` calls. Hope you like it :-) – cmaster - reinstate monica Mar 15 '17 at 15:04
  • Although invasive (and if there are no many functions) maybe using a local object (created at the very beginning of each function) that takes its own address on creation (current stack position) and computes the difference on destruction (using the address of a local variable)... well, just to draft an idea, it has the advantage that memory alignment will be taken into consideration, more difficult with manual counting. – cbuchart Mar 15 '17 at 15:19
  • @cbuchart Yes, I thought into the very same direction myself, but decided that it was not worth the effort of detailing it in the answer, as it's likely not worth the effort to implement it in most settings. If I would need to go for such an approach, I would use a small function that takes the address of a single `char` local variable and compares it to another `char` variables address in `main()`. That has the advantage of including the current function's stack frame in the reported figure, which is omitted by the inserted variable approach. – cmaster - reinstate monica Mar 15 '17 at 17:06
0

You might like gprof. It's often installed with build-essential-like packages, so you may already have it. Here are some basic instructions on using it.

Essentially, you have to re-link your program and then run gprof. It'll generate a textual report that you'll be able to compare for each algorithm you use.

Jack
  • 396
  • 2
  • 18