Advertisement
Guest User

massif demo

a guest
Jun 8th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. /*
  6.  
  7. Demo program:
  8.  gcc -o memfun memfun.c  -lm
  9.  valgrind --tool=massif --massif-out-file=massif.out ./memfun
  10.  massif-visualizer massif.out
  11.  (or ms_print massif.out > massif.txt; less massif.txt)
  12. */
  13.  
  14.  
  15. int main(void)
  16. {
  17.   int ii,jj;
  18.   int *pt;
  19.   int *pt2;
  20.   float ac;
  21.   int numberOfPeaks = 0;
  22.  
  23.   for (ii=0;ii<100;ii++) {
  24.     /* Allocate some small linearly increasing memory block */
  25.     pt = (int *)malloc((1000*ii+1)*sizeof(int));
  26.     if (ii%5 == 0) {
  27.       /* Allocate some gigantic memory block - but only every 10th iteration */
  28.       pt2 = (int *)malloc((1000000)*sizeof(int));
  29.       printf("Allocating large memory block\n");
  30.       numberOfPeaks++;
  31.     } else {
  32.       /* Allocate just one element for every 9/10 iteration */
  33.       pt2 = (int *)malloc((1)*sizeof(int));
  34.     }
  35.     /* Put in data and print it */
  36.     pt2[0] = ii;
  37.     printf("   Found value %i\n",pt2[0]);
  38.     /* Free the memory - note that the peak */
  39.     free(pt2);
  40.  
  41.     /* Spend time on "something" - the actual work does not matter*/
  42.     ac = 1;
  43.     for (jj=0;jj<10000*(1 + ii);jj++) {
  44.       ac += sin(jj*0.01) + cos(jj*0.01 + 0.3);
  45.     }
  46.     printf("   Found dummy value %f\n",ac);
  47.     /* Free the memory */
  48.     free(pt);
  49.   }
  50.   printf("I made %i memory peaks\n",numberOfPeaks);
  51.   return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement