Example 7 Back to GPTL home page

Example 8: Printing max memory usage (RSS) during an application

Here is shown the output from running a simple test code (GPTL/tests/memusage.c), where the appropriate GPTL calls have been made to print when the memory resident set size (RSS) of the process has grown whenever calling GPTLstart() or GPTLstop(). In this simple example the size of the process is made to grow by calling realloc a number of times for some number of MB. Note that RSS would not necessarily grow, were it not for touching the reallocated memory by calling memset.

memusage.c:

#include <gptl.h> #include <stdio.h> #include <string.h> const int onemb = 1024 * 1024; void sub (unsigned char *, int); int main () { int ret; int n; unsigned char *arr; // Print when process size has grown. if ((ret = GPTLsetoption (GPTLdopr_memusage, 1)) != 0) return -1; // Only print when the process has grown by 50% or more since the last print // (or since the process started) if ((ret = GPTLsetoption (GPTLmem_growth, 50)) != 0) return -1; ret = GPTLinitialize (); for (n = 1; n < 10; ++n) sub (arr, n); return 0; } void sub (unsigned char *arr, int n) { unsigned char *space; int ret; ret = GPTLstart ("sub"); space = (unsigned char *) realloc (arr, n*onemb*(sizeof (unsigned char))); arr = space; memset (arr, 0, n*onemb*(sizeof (unsigned char))); ret = GPTLstop ("sub"); }
Now compile and run:
% cc -o memusage memusage.c -I${GPTL}/include -L${GPTL}/lib -lgptl % ./memusage
Here's the important output:
Begin sub RSS grew to 3.35 MB End sub RSS grew to 5.14 MB End sub RSS grew to 8.23 MB End sub RSS grew to 17.26 MB End sub RSS grew to 30.15 MB End sub RSS grew to 47.16 MB

Explanation of the above output

Even though "sub" was called 10 times, only 6 prints of RSS growth occurred. This is because the second call to GPTLsetoption() said to only report when growth from the previous print was by at least 50%. This approach can limit sometimes annoying large volumes of printout when only a tiny increase in RSS occurs.
Example 7 Back to GPTL home page