10

I want to watch a given process in unix over time to see what time memory grows (its a long running job)

I guess I could do

ps -aux | grep PID

and run this from cron every 5 minutes, but it seems there should be a better way.

Is there a way I can use sar to do this?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Joelio
  • 4,621
  • 6
  • 44
  • 80
  • your question belongs on ubuntu or superuser – Daniel A. White Sep 02 '11 at 01:15
  • I was thinking about this, but its from a developers point of view and its a development problem, my process is leaking memory, im not an SA....I guess I can try over there, but it seems relevant on both... – Joelio Sep 02 '11 at 01:42
  • 2
    Duplicate of http://stackoverflow.com/questions/563168/monitor-a-programs-memory-usage-in-linux ? Also suggest using `top` with `-p -b -d` options. – Iterator Sep 02 '11 at 01:53
  • @Joelio - there are tools that can monitor for leaks. – Daniel A. White Sep 02 '11 at 12:19

1 Answers1

21

I use the following:

$ ps -o rss $(pgrep executablename)

Put that in a while loop or use watch to monitor it over time coupled with tail. You can also tee it to a file and have gnuplot plot it for you and refresh it every few seconds.

Piotr Niewinski
  • 1,298
  • 2
  • 15
  • 27
holygeek
  • 15,653
  • 1
  • 40
  • 50
  • Incorrect answer. RSS isn't memory usage. – poige Mar 19 '13 at 18:47
  • 1
    @poige, what do you mean?? - RSS Real-memory (resident set) size in kilobytes of the process. This number is equal to the sum of the number of working segment and code segment pages in memory times 4. Remember that code segment pages are shared among all of the currently running instances of the program. If 26 ksh processes are running, only one copy of any given page of the ksh executable program would be in memory, but the ps command would report that code segment size as part of the RSS of each instance of the ksh program. – Joelio Apr 24 '13 at 12:53
  • Process can be swapped (paged) out partly, and its RSS would be less, actually. – poige Apr 24 '13 at 16:35
  • 1
    If executablename is not running the above is equivalent to just `ps -o rss`. Using `-p` before the PID list seems safer as it asks explicitly for PIDS, thus would fail instead. – Alois Mahdal May 02 '14 at 13:36