Kim Yung
Kim Yung

Reputation: 21

Why is there differenece between Valgrind-Massif and TOPs memory consumption?

I want to measure the memory consumption of a program in linux ubuntu. I compared the two tools: Valgrind Massif and TOP. For some reason, I got different results even when I used "--pages-as-heap=yes" to show all the memory.

  1. I compiled the following code:

    void delay(double secs)
    {
        int i,j;
        for(j=0;j<5000*secs;j++)
            for(i=0;i<99999;i++);
    }
    int main() {
        delay(30);
    return 0; }
    

The TOP command showed a consumption of 4200KB of virtual memory while the program was in the delay function. The Valgrind-Massif tool gave a consumption of 6340608B. Which one is the correct one? And why is there difference?

  1. Although the Massif website tells that with the "--pages-as-heap=yes" option enabled the stack is also measured, I did not have a success with that. For example, for the following program:

    #include <stdlib.h>
    void delay(double secs)
    {
        int i,j;
        for(j=0;j<5000*secs;j++)
            for(i=0;i<99999;i++);
    }
    void func(int n)
    {
       char x[2000000];
       int i;
       if(n==0)
       return;
       for(i=0;i<2000000;i++)
           x[i]=(char)n;
       delay(15);   // Delay number 2,3,4,5
       func(n-1);
       return;
    }
    int main() {
       delay(30);   // Delay number 1
       func(4);
       delay(30);   // Delay number 6
       return 0;}
    

The memory consumption reported by TOP was: 4200 KB, then 6032KB, then 7984 KB, then 9936 KB, then 11892 KB, and then 13844 KB. However, Massif reported only 6336512 B in the begining of the program (in fact, there were some fluctuations in the memory, but they stopped very quickly, and they were not very big). It seems that, for some reason, Massif does not measure the stack under this setting. Why does that happen?

Edit:

I tried to compile in a release mode, but got the same problems. I compiled the following code:

#include<stdio.h>
#include <stdlib.h>
unsigned long delay(double secs)
{   long i,j,k,counter=0;
    for(k=0;k<160000;k++)
    for(j=0;j<5000*secs;j++)
        for(i=0;i<99999;i++)
            counter++;
    return counter;
}
int main() 
{
    unsigned long i;
    i=delay(10);
    printf("%lu\n",i);
    return 0;
}

and still got 4200KB with TOP, and about 6MB with Massif. I compiled the code with the following command:

g++ -O2 -Wall myprog.c -o myprog

Also, when I ran the program I increased the stack size using: ulimit -s 2000000000 and I also increased the stack size when ran the program with Massif, so I won't get stack overflaw. And I still get the same results.

Upvotes: 2

Views: 834

Answers (1)

user7860670
user7860670

Reputation: 37468

It looks like you are trying to profile debug builds (which is completely pointless). Because in release builds both of these programs will have only a dummy main proc as compiler with throw away all the calculations since they have no side effects. Also second program causes stack overflow because you are trying to allocate huge arrays on the stack.

Upvotes: 2

Related Questions