get memory usage of current process on unix


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. """fnl.core.memory module
  2.  
  3. Trivial, but working code to get the memory usage of the current process
  4. where the pid is retrieved using os.getpid() and the memory usage is read
  5. from the unix command ps.
  6. """
  7.  
  8. import os
  9.  
  10. __version__ = "1.0"
  11. __author__ = "Florian Leitner"
  12.  
  13. def mem(size="rss"):
  14. """Generalization; memory sizes: rss, rsz, vsz."""
  15. return int(os.popen('ps -p %d -o %s | tail -1' %
  16. (os.getpid(), size)).read())
  17.  
  18. def rss():
  19. """Return ps -o rss (resident) memory in kB."""
  20. return mem("rss")
  21.  
  22. def rsz():
  23. """Return ps -o rsz (resident + text) memory in kB."""
  24. return mem("rsz")
  25.  
  26. def vsz():
  27. """Return ps -o vsz (virtual) memory in kB."""
  28. return mem("vsz")

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.