Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

One way to do is, catching all calls to heap functions such as malloc, realloc, calloc and then summing up the total size. If you don't have this mechanism in your program, you can still do this without changing your program by using LD_PRELOAD mechanism of linux. You just need to compile a small shared library, which has following functions

 void* malloc (size_t size); void* calloc (size_t num, size_t size); void* realloc (void* ptr, size_t size); 

Then you can implement functions to capture size

 void* malloc (size_t size) { totalCount += size; real_malloc (size_t size) } 

for details of the implementations you can also have a look in the previous answers Overriding malloc with LD_PRELOADOverriding malloc with LD_PRELOAD. Personally I would say just do this mechanism in your program and capture calls internally and count the memory you are allocation.

One way to do is, catching all calls to heap functions such as malloc, realloc, calloc and then summing up the total size. If you don't have this mechanism in your program, you can still do this without changing your program by using LD_PRELOAD mechanism of linux. You just need to compile a small shared library, which has following functions

 void* malloc (size_t size); void* calloc (size_t num, size_t size); void* realloc (void* ptr, size_t size); 

Then you can implement functions to capture size

 void* malloc (size_t size) { totalCount += size; real_malloc (size_t size) } 

for details of the implementations you can also have a look in the previous answers Overriding malloc with LD_PRELOAD. Personally I would say just do this mechanism in your program and capture calls internally and count the memory you are allocation.

One way to do is, catching all calls to heap functions such as malloc, realloc, calloc and then summing up the total size. If you don't have this mechanism in your program, you can still do this without changing your program by using LD_PRELOAD mechanism of linux. You just need to compile a small shared library, which has following functions

 void* malloc (size_t size); void* calloc (size_t num, size_t size); void* realloc (void* ptr, size_t size); 

Then you can implement functions to capture size

 void* malloc (size_t size) { totalCount += size; real_malloc (size_t size) } 

for details of the implementations you can also have a look in the previous answers Overriding malloc with LD_PRELOAD. Personally I would say just do this mechanism in your program and capture calls internally and count the memory you are allocation.

Source Link
edorado
  • 375
  • 2
  • 11

One way to do is, catching all calls to heap functions such as malloc, realloc, calloc and then summing up the total size. If you don't have this mechanism in your program, you can still do this without changing your program by using LD_PRELOAD mechanism of linux. You just need to compile a small shared library, which has following functions

 void* malloc (size_t size); void* calloc (size_t num, size_t size); void* realloc (void* ptr, size_t size); 

Then you can implement functions to capture size

 void* malloc (size_t size) { totalCount += size; real_malloc (size_t size) } 

for details of the implementations you can also have a look in the previous answers Overriding malloc with LD_PRELOAD. Personally I would say just do this mechanism in your program and capture calls internally and count the memory you are allocation.