5

I write my project by c with opencv. I want print info about allocated memory or memory used by my program. Is there a functions, that give me the information about the memory ? Finally I'm using Qt for Linux and Windows ,

Thanks in advance.

Aym
  • 51
  • 2
  • This previous thread has some ideas http://stackoverflow.com/questions/6192531/best-strategy-for-profiling-memory-usage-of-my-code-open-source-and-3rd-party – Ari Sep 21 '12 at 13:20

3 Answers3

4

You can write wrappers to malloc and free that track how much memory you're using.

EDIT: If you also want to intercept calls to malloc and free in external libraries, you will have to define them in a shared library and load it before libc. How you do this depends on your OS.

Dan
  • 12,409
  • 3
  • 50
  • 87
  • if you load a library and that uses the unwrapped `malloc`, is that memory used by your programn or not? – Remus Rusanu Sep 21 '12 at 13:34
  • could you give me example about malloc as wrapper and free . – Aym Sep 22 '12 at 13:55
  • @RemusRusanu it's absolutely possible indeed. One can call OS memory reservation functions directly. like `sbrk` (though unrecommended because it interferes with some malloc implementations). `memmap` which is used by modern mallocs, and gives isolated chunks of mem. `VirtualAlloc` on Win32, same concept. Or the `NTHeap` stuff too. – v.oddou Mar 23 '16 at 12:42
4

On Linux you look into your own process info pseudo-file:

/proc/[pid]/statm
Provides information about memory usage, measured in pages. The columns are:
size       total program size
           (same as VmSize in /proc/[pid]/status)
resident   resident set size
           (same as VmRSS in /proc/[pid]/status)
share      shared pages (from shared mappings)
text       text (code)
lib        library (unused in Linux 2.6)
data       data + stack
dt         dirty pages (unused in Linux 2.6)

On Windows you look at you own process Process Object performance counters:

Private Bytes Shows the current number of bytes that this process has allocated that cannot be shared with other processes.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
-1

You can also do some level of memory analysis of Code/Data segment during build time if you check elf, dump or map file. And GCC command line options for stack usage are: -fstack-usage and -fcallgraph-info.