user1273684
user1273684

Reputation: 1619

trace how much memory gets allocated per function

Is there a tool that allows me to trace how much memory is allocated per function? e.g. all memory allocated by malloc calls whenever my add_node function is called vs those in queue_buffer, etc. for the entire runtime of the program.

I'd like to profile where the bulk of memory is used.

Upvotes: 1

Views: 80

Answers (1)

user3291541
user3291541

Reputation: 11

I don't know of any program to do this offhand, but you can do this yourself. Just add something like this to some global header file:

#ifdef DEBUG
void* __replacement_malloc(size_t bytes, const char* fn_name)
{
        printf("Function %s allocated %lu bytes\n", fn_name, bytes);
        return malloc(bytes);
}
#define malloc(x) __replacement_malloc(x, __func__)
#endif

Now when you compile with "DEBUG" defined, any calls to malloc() will be redirected.

Upvotes: 1

Related Questions