asaf
asaf

Reputation: 976

Getting the max CPU / RAM usage for a process in Linux

How can I get the max CPU / RAM usage in Linux, when starting a process inside my python code ?

I want to calculate from the start of the process till the process end

Upvotes: 0

Views: 1550

Answers (2)

user149341
user149341

Reputation:

"Maximum CPU usage" is essentially meaningless. At any instant in time, a process is either running on one (or more) CPUs, or is not running. As such, the maximum CPU usage of any process will always be at least 100%, because there was at least one instant at which the process is running. Tools which display CPU usage as a percentage are measuring the ratio of CPU time consumed by a process to its age.

The only situation where this might be a useful measure would be for multithreaded processes, where the "maximum CPU usage" is the maximum number of threads belonging to the process which are all running simultaneously. I'm not aware of any specific way to measure this.

Upvotes: 1

Darren Smith
Darren Smith

Reputation: 2498

To get the memory, a very simple way would be to get the output of:

/proc/YOUR_PROGRAM_PID/status

... capture this at the start, and end, of the python process, and extract the memory fields you care about (e.g. VmPeak, VmSize)

Upvotes: 0

Related Questions