Search code examples
linuxmacosmemorymemory-profiling

Memory monitoring on Mac or Linux


Is there any tool to monitor application memory over time and see it as a graph?

For instance, in Windows there is a perfmon. I can easily configure the processes I want to monitor and the resolution and see the graph over time.

Is there something similar in Mac or Linux?

Thanks.


Solution

  • One way is to script it yourself with gnuplot and bash:

    $ cat memory-plot.pg
    #!/usr/bin/gnuplot
    reset
    set terminal dumb 120,40
    set xdata time
    set timefmt "%Y-%m-%dT%H:%M:%S"
    set format x "%H:%M"
    set xlabel "time"
    set ylabel "RSS, KB"
    set title "Process RSS over time."
    set grid
    set style data linespoints
    plot "< tail -n 90 rss.txt" using 1:2 title "RSS"
    
    $ pid=`pgrep Xorg`; clear; while sleep 1; do (printf "%(%Y-%m-%dT%H:%M:%S)T "; ps -ho rss,cmd -p $pid) >> rss.txt; printf "\033[0;0H"; gnuplot memory-plot.pg; done
    

    Outputs a live chart in the terminal:

                                                       Process RSS over time.
    
      105500 +-+--+-----+----+----+-----+----+----+-----+----+-----+----+----+-----+----+----+-----+----+----+-----+--+-+
             +          +         +          +          +          +         +          +          +         +          +
             |          :A        :          :          :          :         :          :          :        RSS +--A--+ |
             |          :|        :          :          :          :         :          :          :         :          |
             |          :|      A : -A     A :          :          :         :          :          :         :          |
      105000 +-+.........|......|..A.|....A|..........................................................................+-+
             |          :|      | :| |    || :          :          :         :          :          :         :          |
             |          :|      |||  |    || :   AA     :          :         :          :          :         :          |
             |          :|      |||  |    || :   ||     :          :         :          :          :         :          |
      104500 +-+.........|.....|.|...|....||....A..|..................................................................+-+
             |          :||    | A:  |    || :  |  |    :          :         :          :          :         :          |
             |          :||    |  :  |   | | :  |   |   :          :         :          :          :         :          |
             |          :||    |  :  |   | | :  |   A   :          :         :          :          :         :          |
             |          :||    A  :  |   | | :  |   |   :          :         :          :          :         :          |
      104000 +-+.........||.AAA.......|..|..|...|...|.................................................................+-+
             |          :||A      :   |  |  |: |    |   :          :         :          :          :         :          |
             |          :|||      :   | AA  |: |    |   :          :         :          :          :         :          |
             |          | |       :   |A    |: |     |  :          :         :          :          :         :          |
             |          | A       :   ||    |: |     |  :          :         :          :          :         :          |
      103500 +-+........|.............||....|..|.....|................................................................+-+
             |          |         :   ||    |: A     |AAAAAAAAAAAA-AAAAAAAAAAAA-AA-AAAAAAAAAAAAAA-AAAAAAAAAAAAA         |
             |          |         :   |     |: |     || :          :         :          :          :         :          |
             |          |         :   |     |:|      A  :          :         :          :          :         :          |
             |          |         :   A     AAA         :          :         :          :          :         :          |
      103000 +-+........|.............................................................................................+-+
             |          |         :          :          :          :         :          :          :         :          |
             |          |         :          :          :          :         :          :          :         :          |
             |          |         :          :          :          :         :          :          :         :          |
      102500 +-+........|.............................................................................................+-+
             |          |         :          :          :          :         :          :          :         :          |
             |          |         :          :          :          :         :          :          :         :          |
             AAAAAAAA-AAA         :          :          :          :         :          :          :         :          |
             +          +         +          +          +          +         +          +          +         +          +
      102000 +-+--+-----+----+----+-----+----+----+-----+----+-----+----+----+-----+----+----+-----+----+----+-----+--+-+
           11:49      11:49     11:49      11:49      11:49      11:49     11:50      11:50      11:50     11:50      11:50