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.
This is data for which memory is being allocated while executing the program. In C++, this allocation is usually performed by the malloc function or new operator. In 32-bit programs the size of dynamically allocated memory is restricted to 2 Gbytes, in 64-bit programs to 8 Tbytes.
Q) How can you determine the size of an allocated portion of memory? In C language, we can calculate the size of the static array using the sizeof operator but there is no operator to calculate the size of the dynamically allocated memory. So using some trick we can get the size of the allocated array.
It is impossible to know how much memory was allocated by just the pointer. doing sizeof (p) will get the size of the pointer variable p which it takes at compile time, and which is the size of the pointer. That is, the memory the pointer variable takes to store the pointer variable p .
Linux glibc sysconf(_SC_AVPHYS_PAGES) and get_avphys_pages() These two glibc extensions should give you the available number of pages. We can then just multiply that by the pages size sysconf(_SC_PAGESIZE) to find the total available memory in bytes.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With