简体   繁体   English

如何获得我的程序分配的内存大小?

[英]How can i get memory size allocated by my program?

I write my project by c with opencv. 我用opencv用c编写了我的项目。 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 , 最后我使用Qt for Linux和Windows,

Thanks in advance. 提前致谢。

You can write wrappers to malloc and free that track how much memory you're using. 您可以将包装器写入mallocfree ,以跟踪您正在使用的内存量。

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. 编辑:如果你还想拦截对malloc的调用和外部库中的free,你必须在共享库中定义它们并在libc之前加载它。 How you do this depends on your OS. 如何执行此操作取决于您的操作系统。

On Linux you look into your own process info pseudo-file : 在Linux上,您将查看自己的进程信息伪文件

/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: 在Windows上,您可以查看自己的进程Process Object性能计数器:

Private Bytes Shows the current number of bytes that this process has allocated that cannot be shared with other processes. Private Bytes显示此进程分配的当前字节数,该字节数无法与其他进程共享。

You can also do some level of memory analysis of Code/Data segment during build time if you check elf, dump or map file. 如果检查elf,dump或map文件,还可以在构建期间对代码/数据段进行某种程度的内存分析。 And GCC command line options for stack usage are: -fstack-usage and -fcallgraph-info. 堆栈使用的GCC命令行选项是: -fstack-usage和-fcallgraph-info。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM