C程序的内存分析 [英] memory profiling for C program

查看:80
本文介绍了C程序的内存分析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要对我的C应用程序进行内存分析.

Need to do a memory profiling of my C application ..

它应该包括占用空间大小和RAM大小...

It should include footprint size and a RAM size ...

例如,如果我的应用程序如下所示.

for example if my application is like below ..

#include <stdio.h>

int global = 10; /* initialized global variable */

int test_code(void)
{
    static int i = 100; /* Initialized static variable*/
    return 0;
}

输出:

[putta@linux]$ gcc memory-layout.c -c memory-layout 

[putta@linux]$ ls -ltrh  memory-layout.o
760 Nov  9 18:26 memory-layout

[putta@linux]$ size memory-layout.o
   text    data     bss     dec     hex filename
     67       8       0      75      4b memory-layout.o

因此,现在应该考虑使用哪个内存进行性能分析,以及加载程序时的RAM ..

So now which memory I should be considered for profiling footprint, and RAM when the program is loaded ..

下面的配置文件正确吗? 占用内存= 760(位于闪存或硬盘上) RAM = 67 + 8 + 0 = 75字节

is the below profiling is correct? footprint memory = 760 (which is sits flash or harddisk) RAM = 67+8+0 = 75 Bytes

需要专家的建议

推荐答案

查找对象的内存大小

如果要了解磁盘上程序的大小以及RAM中文本和数据的大小,在Linux/Unix上,可以使用size命令:

Find the memory size of the object

If you want to know the size of your program on disk plus the size of text and data in RAM, on Linux/Unix you can use the size command:

$> size /bin/cat
text       data     bss     dec     hex filename
43422      1720    2472   47614    b9fe /bin/cat

size的输出是目标文件不同部分的内存大小:

The outputs of size are the memory sizes of different parts of the object file:

  • 文本 :(代码段)可执行指令
  • 数据 :(数据段)已初始化的全局变量
  • bss :(由符号开头的块)静态分配的变量
  • text: (code segment) executable instructions
  • data: (data segment) initialised global variables
  • bss: (block started by symbols) statically-allocated variables

最后两列 dec hex 是十进制和十六进制中其他三个(总大小)的总和.

The last two columns, dec and hex, are respectively the sum of the other three (the overall size) in decimal and hexadecimal.

您要的大小是: ls 的输出(给出磁盘上的大小)加上输出的 dec 部分size 命令,可为您提供RAM上的大小.

The size you are asking for is: the output of ls (that gives you the size on disk) plus the dec part of the output of the size command that gives you the size on RAM.

另请参阅这些帖子:如何知道我的二进制可执行文件的内存占用量

See also these posts: http://www.cyberciti.biz/faq/linux-find-size-of-text-data-segment-bss-uninitialized-data/, how to know the memory footprint of my binary executable

在引用软件应用程序时,占地面积指示正在运行的进程消耗的内存大小(运行时内存要求).

When referring to a software application the footprint indicates the size of the memory consumed by the running process (runtime memory requirements).

表示,很明显,在进程运行时应检查内存占用量.我认为(以及其他帖子确认),唯一真正的选择是使用诸如 valgrind 之类的工具.

Said that, it is clear that you should check the memory footprint when the process is running. I think (and other posts confirm it) that the only real option is to use a tool like valgrind.

使用valgrind配置您的应用程序

您可以使用 Massif 工具分析内存. Massif 是一个 heap 分析器,但也可以测量 stack 的大小.

You can profile the memory using the Massif tool. Massif is an heap profiler but can also measure the size of the stack.

valgrind --tool = massif --stacks =是

valgrind --tool=massif --stacks=yes

这将为您提供 heap stack 的内存使用量. 然后将信息存储在文件massif.out中. 您可以使用

This will give you both the heap and stack memory usage. Then the information are stored in the file massif.out.???? that you can read with

ms_print massif.out.?????

ms_print massif.out.?????

文件中的第一个输出是运行期间内存使用情况的漂亮图表.

The first output in the file is a nice chart of the memory usage during the running time.

--------------------------------------------------------------------------------
Command:            ./myprog -f d5.ini
Massif arguments:   --stacks=yes
ms_print arguments: massif.out.24377
--------------------------------------------------------------------------------


    MB
5.292^                                                    ##                  
     |    @                 :           :  @@   :      :  # ::::   :  :       
     |    @:::: ::    :   :@:@@::::::::::::@ :::::::::::::# ::::@::::@::::::::
     |    @:: ::: :::::::::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     |    @:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     |    @:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     |    @:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     |    @:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     |    @:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     |    @:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     |   @@:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     |   @@:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     | ::@@:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     | : @@:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     | : @@:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     | : @@:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     | : @@:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     | : @@:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     | : @@:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
     | : @@:: ::: : :::: ::@:@ ::: :: :::: @ :: ::: ::::::# ::::@: ::@::::::::
   0 +----------------------------------------------------------------------->Gi
     0                                                                   1.030

详细信息存储在文件中的不同表中. 要完全理解输出,请参阅Valgrind手册页面似乎很清楚.

The details are stored in the file, inside different tables. To fully understand the output refers to the Valgrind manual page which seems really clear.

跟踪子项的选项是:--trace-children=yes

有趣的是,似乎没有"进程的实际内存使用情况": https://unix.stackexchange.com/questions/164653/actual-memory-流程的使用.

Interesting, it seems that there is no "actual memory usage of a process": https://unix.stackexchange.com/questions/164653/actual-memory-usage-of-a-process.

这篇关于C程序的内存分析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆