1

I need to create a Python Script that Returns only the Process using the most memory at that time. I am having problems with parsing the values. What I have so far:

import psutil

x = psutil.pids()
for i in x:
p = psutil.Process(i)
print(p.name(), p.memory_full_info())

This returns the full list of processes in this format:

 ssh-agent pfullmem(rss=339968, vms=11350016, shared=0, text=352256, lib=0, data=421888, dirty=0, uss=598016, pss=605184, swap=0)

I am getting value errors whenever I try to manipulate these values. I want to just return the MOST memory intensive process and perhaps the RSS or VMS value.

1
  • What kind of errors do you get? Commented Apr 23, 2017 at 16:55

2 Answers 2

1
import psutil

pids = psutil.pids()
processes = map(psutil.Process, pids)
most_mem_process = max(processes, key=lambda p: p.memory_full_info().data)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @EyuelDK that go me most of the way there. The only thing I am still missing is the uss number that would list the amount of memory. I have gotten this far in isolating the number: jk = psutil.Process(i).memory_percent(memtype='uss') that will take the pid as 'i' and return the uss number.
I'm not very familiar with the psutil library, but if you want to filter based on the uss just replace p.memory_full_info().data with p.memory_full_info().uss. In the end, most_mem_process, i.e. a Process object, has all the info you need, including the uss
1
import subprocess
subprocess.check_output('ps -eo pmem,cmd | sort -k 1 -nr | head -1', shell=True).decode('utf-8').replace('\n', '')

This command returns two things: The first one, is the percentage of memory usage of the process and the second one is the running process with highest memory usage.

1 Comment

Please add more information and context. What does this code do and how does this answer the user's question? If you're going to make a code-only answer, the code needs to at least be well commented.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.