How to measure actual memory usage of an applicati

2018-12-31 06:51发布

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 article of Understanding memory usage on Linux, ps is not an accurate tool to use for this intent.

Why ps is "wrong"

Depending on how you look at it, ps is not reporting the real memory usage of processes. What it is really doing is showing how much real memory each process would take up if it were the only process running. Of course, a typical Linux machine has several dozen processes running at any given time, which means that the VSZ and RSS numbers reported by ps are almost definitely wrong.

30条回答
像晚风撩人
2楼-- · 2018-12-31 07:21
#!/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
查看更多
骚的不知所云
3楼-- · 2018-12-31 07:22

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

查看更多
浪荡孟婆
4楼-- · 2018-12-31 07:23

There is no easy way to calculate this. But some people have tried to get some good answers:

查看更多
美炸的是我
5楼-- · 2018-12-31 07:26

This is an excellent summary of the tools and problems: archive.org link

I'll quote it, so that more devs will actually read it.

If you want to analyse memory usage of the whole system or to thoroughly analyse memory usage of one application (not just its heap usage), use exmap. For whole system analysis, find processes with the highest effective usage, they take the most memory in practice, find processes with the highest writable usage, they create the most data (and therefore possibly leak or are very ineffective in their data usage). Select such application and analyse its mappings in the second listview. See exmap section for more details. Also use xrestop to check high usage of X resources, especially if the process of the X server takes a lot of memory. See xrestop section for details.

If you want to detect leaks, use valgrind or possibly kmtrace.

If you want to analyse heap (malloc etc.) usage of an application, either run it in memprof or with kmtrace, profile the application and search the function call tree for biggest allocations. See their sections for more details.

查看更多
步步皆殇っ
6楼-- · 2018-12-31 07:26

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.

查看更多
浪荡孟婆
7楼-- · 2018-12-31 07:26

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 in 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 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
查看更多
登录 后发表回答