How can I measure the actual memory usage of an application or process?

后端 未结 30 2714
心在旅途
心在旅途 2020-11-22 03:01

This question is covered here in great detail.

How do you measure the memory usage of an application or process in Linux?

From the blog articl

相关标签:
30条回答
  • 2020-11-22 03:41
    #!/bin/ksh
    #
    # Returns total memory used by process $1 in kb.
    #
    # See /proc/NNNN/smaps if you want to do something
    # more interesting.
    #
    
    IFS=$'\n'
    
    for line in $(</proc/$1/smaps)
    do
       [[ $line =~ ^Size:\s+(\S+) ]] && ((kb += ${.sh.match[1]}))
    done
    
    print $kb
    
    0 讨论(0)
  • 2020-11-22 03:41
    • Last but not least use htop.
    $ sudo apt-get update
    $ sudo apt-get install htop
    
    • Run htop using below command.
    $ htop
    
    • Note for newcomers: dollar symbol is used to show we are running command on a terminal
    0 讨论(0)
  • 2020-11-22 03:42

    While this question seems to be about examining currently running processes, I wanted to see the peak memory used by an application from start to finish. Besides Valgrind, you can use tstime, which is much simpler. It measures the "highwater" memory usage (RSS and virtual). From this answer.

    0 讨论(0)
  • 2020-11-22 03:43

    With ps or similar tools you will only get the amount of memory pages allocated by that process. This number is correct, but:

    • does not reflect the actual amount of memory used by the application, only the amount of memory reserved for it

    • can be misleading if pages are shared, for example by several threads or by using dynamically linked libraries

    If you really want to know what amount of memory your application actually uses, you need to run it within a profiler. For example, Valgrind can give you insights about the amount of memory used, and, more importantly, about possible memory leaks in your program. The heap profiler tool of Valgrind is called 'massif':

    Massif is a heap profiler. It performs detailed heap profiling by taking regular snapshots of a program's heap. It produces a graph showing heap usage over time, including information about which parts of the program are responsible for the most memory allocations. The graph is supplemented by a text or HTML file that includes more information for determining where the most memory is being allocated. Massif runs programs about 20x slower than normal.

    As explained in the Valgrind documentation, you need to run the program through Valgrind:

    valgrind --tool=massif <executable> <arguments>
    

    Massif writes a dump of memory usage snapshots (e.g. massif.out.12345). These provide, (1) a timeline of memory usage, (2) for each snapshot, a record of where in your program memory was allocated. A great graphical tool for analyzing these files is massif-visualizer. But I found ms_print, a simple text-based tool shipped with Valgrind, to be of great help already.

    To find memory leaks, use the (default) memcheck tool of valgrind.

    0 讨论(0)
  • 2020-11-22 03:43

    The below command line will give you the total memory used by the various process running on the Linux machine in MB:

    ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | awk '{total=total + $1} END {print total}'
    
    0 讨论(0)
  • 2020-11-22 03:44

    Three more methods to try:

    1. ps aux --sort pmem
      It sorts the output by %MEM.
    2. ps aux | awk '{print $2, $4, $11}' | sort -k2r | head -n 15
      It sorts using pipes.
    3. top -a
      It starts top sorting by %MEM

    (Extracted from here)

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