0

I am working in a C++ program in Linux. Now I want to check how the memory is allocated in my program. Since the Library I am using is complex, I cannot estimate manually.

I googled on online. Somebody suggests valgrind. I used it, but it crashes my program. Also somebody use getrusage (http://linux.die.net/man/2/getrusage), but I found many negative comments on that.

Anybody has suggestions on that?

Lv Zhaoyang
  • 107
  • 1
  • 4
  • 10

2 Answers2

0

SIGAR has been recommended before

It seems to give mostly total memory/cpu usage during runtime but may be useful as it has bindings for many languages and works on many platforms.

As for more detailed per-process information, you can get resident, shared, virtual memory totals as well as i/o and page faults.

Community
  • 1
  • 1
Josh S.
  • 128
  • 8
0

If your memory is allocated by malloc, then from gdb (or your code):

(gdb) call malloc_stats()

http://www.gnu.org/software/libc/manual/html_node/Statistics-of-Malloc.html

3.2.2.11 Statistics for Memory Allocation with malloc

You can get information about dynamic memory allocation by calling the mallinfo function. This function and its associated data type are declared in malloc.h; they are an extension of the standard SVID/XPG version.

— Data Type: struct mallinfo
This structure type is used to return information about the dynamic memory allocator. It contains the following members:

int arena
This is the total size of memory allocated with sbrk by malloc, in bytes.
int ordblks
This is the number of chunks not in use. (The memory allocator internally gets chunks of memory from the operating system, and then carves them up to satisfy individual malloc requests; see Efficiency and Malloc.)
int smblks
This field is unused.
int hblks
This is the total number of chunks allocated with mmap.
int hblkhd
This is the total size of memory allocated with mmap, in bytes.
int usmblks
This field is unused.
int fsmblks
This field is unused.
int uordblks
This is the total size of memory occupied by chunks handed out by malloc.
int fordblks
This is the total size of memory occupied by free (not in use) chunks.
int keepcost
This is the size of the top-most releasable chunk that normally borders the end of the heap (i.e., the high end of the virtual address space's data segment).
— Function: struct mallinfo mallinfo (void)
This function returns information about the current dynamic memory usage in a structure of type struct mallinfo.

Bruce
  • 2,230
  • 18
  • 34