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

后端 未结 30 2661
心在旅途
心在旅途 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:46

    I'm using htop; it's a very good console program similar to Windows Task Manager.

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

    Try the pmap command:

    sudo pmap -x <process pid>
    
    0 讨论(0)
  • 2020-11-22 03:49

    Beside the solutions listed in the answers, you can use the Linux command "top". It provides a dynamic real-time view of the running system, and it gives the CPU and memory usage for the whole system, along with for every program, in percentage:

    top
    

    to filter by a program PID:

    top -p <PID>
    

    To filter by a program name:

    top | grep <PROCESS NAME>
    

    "top" provides also some fields such as:

    VIRT -- Virtual Image (kb): The total amount of virtual memory used by the task

    RES -- Resident size (kb): The non-swapped physical memory a task has used ; RES = CODE + DATA.

    DATA -- Data+Stack size (kb): The amount of physical memory devoted to other than executable code, also known as the 'data resident set' size or DRS.

    SHR -- Shared Mem size (kb): The amount of shared memory used by a task. It simply reflects memory that could be potentially shared with other processes.

    Reference here.

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

    If your code is in C or C++ you might be able to use getrusage() which returns you various statistics about memory and time usage of your process.

    Not all platforms support this though and will return 0 values for the memory-use options.

    Instead you can look at the virtual file created in /proc/[pid]/statm (where [pid] is replaced by your process id. You can obtain this from getpid()).

    This file will look like a text file with 7 integers. You are probably most interested in the first (all memory use) and sixth (data memory use) numbers in this file.

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

    Based on answer to a related question.

    You may use SNMP to get the memory and CPU usage of a process in a particular device on the network :)

    Requirements:

    • the device running the process should have snmp installed and running
    • snmp should be configured to accept requests from where you will run the script below (it may be configured in file snmpd.conf)
    • you should know the process ID (PID) of the process you want to monitor

    Notes:

    • HOST-RESOURCES-MIB::hrSWRunPerfCPU is the number of centi-seconds of the total system's CPU resources consumed by this process. Note that on a multi-processor system, this value may increment by more than one centi-second in one centi-second of real (wall clock) time.

    • HOST-RESOURCES-MIB::hrSWRunPerfMem is the total amount of real system memory allocated to this process.

    **

    Process monitoring script:

    **

    echo "IP: "
    read ip
    echo "specfiy pid: "
    read pid
    echo "interval in seconds:"
    read interval
    
    while [ 1 ]
    do
        date
        snmpget -v2c -c public $ip HOST-RESOURCES-MIB::hrSWRunPerfCPU.$pid
        snmpget -v2c -c public $ip HOST-RESOURCES-MIB::hrSWRunPerfMem.$pid
        sleep $interval;
    done
    
    0 讨论(0)
  • 2020-11-22 03:50

    Get Valgrind. Give it your program to run, and it'll tell you plenty about its memory usage.

    This would apply only for the case of a program that runs for some time and stops. I don't know if Valgrind can get its hands on an already-running process or shouldn't-stop processes such as daemons.

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