memory profiling for C program

后端 未结 2 1604
盖世英雄少女心
盖世英雄少女心 2021-02-09 04:41

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

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

for example if my application is like below ..



        
相关标签:
2条回答
  • 2021-02-09 05:17

    This program is undefined: there is no main function.

    A compiler reserves the right not to compile anything in this case, so resulting in a zero footprint and memory size.

    0 讨论(0)
  • 2021-02-09 05:27

    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
    

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

    • text: (code segment) executable instructions
    • data: (data segment) initialised global variables
    • bss: (block started by symbols) statically-allocated variables

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

    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

    Find the memory footprint

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

    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.

    Profile your application with valgrind

    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=yes

    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.?????

    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
    

    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.

    The option to trace the children is: --trace-children=yes

    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.

    0 讨论(0)
提交回复
热议问题