Quote:
> > Is there any way to measure how much memory a specific function (or
> > program) is using ?
> Code or data?
Automatic data or from the free-store? Actual memory pages or virtual
memory pages? If the code segments are memory mapped, do we count the
loaded pages or all the pages? Is it a peak or average figure that is
wanted?
An evil and cheesy hack to find an approximation of the current size of
automatic memory (highly inaccurate, of course...):
/*
** If questioned, Dann Corbit will deny any knowledge of
** this program's existence.
*/
#include <stdio.h>
char *g_end_I_am_a_cheesy_hack;
char *g_beg_I_am_a_cheesy_hack;
double g_distance;
void stack_eater(int d)
{
long consume[2000];
g_end_I_am_a_cheesy_hack = (char *) consume;
g_end_I_am_a_cheesy_hack += sizeof consume;
/* May need to reverse this if stack grows other way: */
g_distance = g_beg_I_am_a_cheesy_hack - g_end_I_am_a_cheesy_hack;
printf("%.0f bytes of stack gobbled, roughly.\n", g_distance);
if (d < 100)
stack_eater(++d);
Quote:
}
int main(void)
{
char I_am_the_first_thing_on_the_pile; /* This can't move
*/
g_beg_I_am_a_cheesy_hack = &I_am_the_first_thing_on_the_pile;
stack_eater(0);
return 0;
Quote:
}
--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
"The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm