How to calculate the memory consumed by a “C” program in linux

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












5















I have written two C programmes



  1. one is using function pointer, and

  2. the other without function pointer.

Now i want to know the memory consumed by the two programmes , to see how memory can optimized.










share|improve this question




























    5















    I have written two C programmes



    1. one is using function pointer, and

    2. the other without function pointer.

    Now i want to know the memory consumed by the two programmes , to see how memory can optimized.










    share|improve this question


























      5












      5








      5


      5






      I have written two C programmes



      1. one is using function pointer, and

      2. the other without function pointer.

      Now i want to know the memory consumed by the two programmes , to see how memory can optimized.










      share|improve this question
















      I have written two C programmes



      1. one is using function pointer, and

      2. the other without function pointer.

      Now i want to know the memory consumed by the two programmes , to see how memory can optimized.







      memory performance cpu c optimization






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 3 '15 at 6:37









      shivams

      2,94111425




      2,94111425










      asked Jun 3 '15 at 6:07









      NagrajNagraj

      26112




      26112




















          4 Answers
          4






          active

          oldest

          votes


















          5














          It would depend on what kind of stats you want, but if you're writing a program in C running on Linux, you'd definitely better know about Valgrind.



          Valgrind can, not only profile detailed memory usage of your program, but also detect memory access violations which are common in C and possibly very hard to debug.



          For your profiling purpose, take a look at docs about specific analysis tools, especially memcheck and massif.






          share|improve this answer






























            4














            If you are only interested in the memory used after the fact, then use GNU time:



            command time -v myprogram


            (the above uses the bash way of invoking the external time command rather than the bash builtin, your shell may vary).



            Or, GNU memusage:



            memusage -T ./myprogram


            If you are interested in the memory used on an ongoing basis (i.e. during a long running process), one of the other answers is probably better.
            See also this related question: Memory usage command with syntax similar to the time command






            share|improve this answer
































              3














              Here's the resident set size and virtual memory size of all sshd processes on one system:



              ulric@qvp2:~$ ps -eo rss,vsz,args|grep sshd|grep -v grep
              448 55292 /usr/sbin/sshd -D
              5176 147460 sshd: ulric [priv]
              2776 149704 sshd: ulric@pts/3


              Or perhaps easier:



              ulric@qvp2:~$ ps aux|head -n 1&&ps aux|grep sshd|grep -v grep
              USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
              root 13221 0.0 0.0 55292 448 ? Ss Apr21 0:01 /usr/sbin/sshd -D
              root 16046 0.0 0.5 147460 5176 ? Ss 08:12 0:00 sshd: ulric [priv]
              ulric 16187 0.0 0.2 149704 2776 ? S 08:12 0:00 sshd: ulric@pts/3


              See the ps manpage for more options.






              share|improve this answer






























                -2














                The easiest to just catch the heap pointers through sbrk(0), cast them as 64-bit unsigned integers, and compute the difference after the memory gets allocated.






                share|improve this answer






















                  Your Answer








                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "106"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: false,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f207209%2fhow-to-calculate-the-memory-consumed-by-a-c-program-in-linux%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  5














                  It would depend on what kind of stats you want, but if you're writing a program in C running on Linux, you'd definitely better know about Valgrind.



                  Valgrind can, not only profile detailed memory usage of your program, but also detect memory access violations which are common in C and possibly very hard to debug.



                  For your profiling purpose, take a look at docs about specific analysis tools, especially memcheck and massif.






                  share|improve this answer



























                    5














                    It would depend on what kind of stats you want, but if you're writing a program in C running on Linux, you'd definitely better know about Valgrind.



                    Valgrind can, not only profile detailed memory usage of your program, but also detect memory access violations which are common in C and possibly very hard to debug.



                    For your profiling purpose, take a look at docs about specific analysis tools, especially memcheck and massif.






                    share|improve this answer

























                      5












                      5








                      5







                      It would depend on what kind of stats you want, but if you're writing a program in C running on Linux, you'd definitely better know about Valgrind.



                      Valgrind can, not only profile detailed memory usage of your program, but also detect memory access violations which are common in C and possibly very hard to debug.



                      For your profiling purpose, take a look at docs about specific analysis tools, especially memcheck and massif.






                      share|improve this answer













                      It would depend on what kind of stats you want, but if you're writing a program in C running on Linux, you'd definitely better know about Valgrind.



                      Valgrind can, not only profile detailed memory usage of your program, but also detect memory access violations which are common in C and possibly very hard to debug.



                      For your profiling purpose, take a look at docs about specific analysis tools, especially memcheck and massif.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jun 3 '15 at 7:01









                      yaegashiyaegashi

                      8,43611734




                      8,43611734























                          4














                          If you are only interested in the memory used after the fact, then use GNU time:



                          command time -v myprogram


                          (the above uses the bash way of invoking the external time command rather than the bash builtin, your shell may vary).



                          Or, GNU memusage:



                          memusage -T ./myprogram


                          If you are interested in the memory used on an ongoing basis (i.e. during a long running process), one of the other answers is probably better.
                          See also this related question: Memory usage command with syntax similar to the time command






                          share|improve this answer





























                            4














                            If you are only interested in the memory used after the fact, then use GNU time:



                            command time -v myprogram


                            (the above uses the bash way of invoking the external time command rather than the bash builtin, your shell may vary).



                            Or, GNU memusage:



                            memusage -T ./myprogram


                            If you are interested in the memory used on an ongoing basis (i.e. during a long running process), one of the other answers is probably better.
                            See also this related question: Memory usage command with syntax similar to the time command






                            share|improve this answer



























                              4












                              4








                              4







                              If you are only interested in the memory used after the fact, then use GNU time:



                              command time -v myprogram


                              (the above uses the bash way of invoking the external time command rather than the bash builtin, your shell may vary).



                              Or, GNU memusage:



                              memusage -T ./myprogram


                              If you are interested in the memory used on an ongoing basis (i.e. during a long running process), one of the other answers is probably better.
                              See also this related question: Memory usage command with syntax similar to the time command






                              share|improve this answer















                              If you are only interested in the memory used after the fact, then use GNU time:



                              command time -v myprogram


                              (the above uses the bash way of invoking the external time command rather than the bash builtin, your shell may vary).



                              Or, GNU memusage:



                              memusage -T ./myprogram


                              If you are interested in the memory used on an ongoing basis (i.e. during a long running process), one of the other answers is probably better.
                              See also this related question: Memory usage command with syntax similar to the time command







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Apr 13 '17 at 12:37









                              Community

                              1




                              1










                              answered Jun 3 '15 at 9:13









                              mr.spuraticmr.spuratic

                              6,9911128




                              6,9911128





















                                  3














                                  Here's the resident set size and virtual memory size of all sshd processes on one system:



                                  ulric@qvp2:~$ ps -eo rss,vsz,args|grep sshd|grep -v grep
                                  448 55292 /usr/sbin/sshd -D
                                  5176 147460 sshd: ulric [priv]
                                  2776 149704 sshd: ulric@pts/3


                                  Or perhaps easier:



                                  ulric@qvp2:~$ ps aux|head -n 1&&ps aux|grep sshd|grep -v grep
                                  USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
                                  root 13221 0.0 0.0 55292 448 ? Ss Apr21 0:01 /usr/sbin/sshd -D
                                  root 16046 0.0 0.5 147460 5176 ? Ss 08:12 0:00 sshd: ulric [priv]
                                  ulric 16187 0.0 0.2 149704 2776 ? S 08:12 0:00 sshd: ulric@pts/3


                                  See the ps manpage for more options.






                                  share|improve this answer



























                                    3














                                    Here's the resident set size and virtual memory size of all sshd processes on one system:



                                    ulric@qvp2:~$ ps -eo rss,vsz,args|grep sshd|grep -v grep
                                    448 55292 /usr/sbin/sshd -D
                                    5176 147460 sshd: ulric [priv]
                                    2776 149704 sshd: ulric@pts/3


                                    Or perhaps easier:



                                    ulric@qvp2:~$ ps aux|head -n 1&&ps aux|grep sshd|grep -v grep
                                    USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
                                    root 13221 0.0 0.0 55292 448 ? Ss Apr21 0:01 /usr/sbin/sshd -D
                                    root 16046 0.0 0.5 147460 5176 ? Ss 08:12 0:00 sshd: ulric [priv]
                                    ulric 16187 0.0 0.2 149704 2776 ? S 08:12 0:00 sshd: ulric@pts/3


                                    See the ps manpage for more options.






                                    share|improve this answer

























                                      3












                                      3








                                      3







                                      Here's the resident set size and virtual memory size of all sshd processes on one system:



                                      ulric@qvp2:~$ ps -eo rss,vsz,args|grep sshd|grep -v grep
                                      448 55292 /usr/sbin/sshd -D
                                      5176 147460 sshd: ulric [priv]
                                      2776 149704 sshd: ulric@pts/3


                                      Or perhaps easier:



                                      ulric@qvp2:~$ ps aux|head -n 1&&ps aux|grep sshd|grep -v grep
                                      USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
                                      root 13221 0.0 0.0 55292 448 ? Ss Apr21 0:01 /usr/sbin/sshd -D
                                      root 16046 0.0 0.5 147460 5176 ? Ss 08:12 0:00 sshd: ulric [priv]
                                      ulric 16187 0.0 0.2 149704 2776 ? S 08:12 0:00 sshd: ulric@pts/3


                                      See the ps manpage for more options.






                                      share|improve this answer













                                      Here's the resident set size and virtual memory size of all sshd processes on one system:



                                      ulric@qvp2:~$ ps -eo rss,vsz,args|grep sshd|grep -v grep
                                      448 55292 /usr/sbin/sshd -D
                                      5176 147460 sshd: ulric [priv]
                                      2776 149704 sshd: ulric@pts/3


                                      Or perhaps easier:



                                      ulric@qvp2:~$ ps aux|head -n 1&&ps aux|grep sshd|grep -v grep
                                      USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
                                      root 13221 0.0 0.0 55292 448 ? Ss Apr21 0:01 /usr/sbin/sshd -D
                                      root 16046 0.0 0.5 147460 5176 ? Ss 08:12 0:00 sshd: ulric [priv]
                                      ulric 16187 0.0 0.2 149704 2776 ? S 08:12 0:00 sshd: ulric@pts/3


                                      See the ps manpage for more options.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jun 3 '15 at 6:26









                                      Ulric ErikssonUlric Eriksson

                                      21416




                                      21416





















                                          -2














                                          The easiest to just catch the heap pointers through sbrk(0), cast them as 64-bit unsigned integers, and compute the difference after the memory gets allocated.






                                          share|improve this answer



























                                            -2














                                            The easiest to just catch the heap pointers through sbrk(0), cast them as 64-bit unsigned integers, and compute the difference after the memory gets allocated.






                                            share|improve this answer

























                                              -2












                                              -2








                                              -2







                                              The easiest to just catch the heap pointers through sbrk(0), cast them as 64-bit unsigned integers, and compute the difference after the memory gets allocated.






                                              share|improve this answer













                                              The easiest to just catch the heap pointers through sbrk(0), cast them as 64-bit unsigned integers, and compute the difference after the memory gets allocated.







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Apr 4 '17 at 15:13









                                              Kakash1hatakeKakash1hatake

                                              11




                                              11



























                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f207209%2fhow-to-calculate-the-memory-consumed-by-a-c-program-in-linux%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown






                                                  Popular posts from this blog

                                                  Peggy Mitchell

                                                  Palaiologos

                                                  The Forum (Inglewood, California)