How to measure memory consumption of a program on Linux

According to the hard fight which is a comparison of Java, Python and Ruby side by side, the performance is very impressive because Python is faster than the rest. A java guru immediately identified flaws of this test in Java codes. In addition, he also improved the codes for getting faster program. However, he was too busy to perform the benchmarks again himself. I volunteered to do this task on my laptop. After preparing all codes in Java, Python and Ruby, I have to measure how long it takes to execute each code. The tool I used was "time". Everything went well until I was asked to also measure memory consumption in each case. time always gave me zero without any error message. It seems this is a bug in Linux kernel that getrusage() will return zero in all memory related values. Fortunately, I found a workaround using Python!

The problem is getrusage() and wait3() in Linux but we also have /proc. Everything we need are already available in /proc//stat and /proc//statm. In other words, memory consumption statistics are reported in /proc so we just need to read them. Anyway, we don't need to do it ourselves since we may use proc-time.py. As a result, measuring memory consumption is as simple as follow.

$ ./proc-time.py java hash
 
user:   1.33(133) system:   0.07(7)
elapsed:  3.462M
MAXSIZE: 254.492M(65150), MAXRSS: 10.148M(2598)
AVGSIZE: 160.414M(41066), AVGRSS:  6.043M(1547)
$ ./proc-time.py python hash.py
 
user:   1.97(197) system:   0.06(6)
elapsed:  2.070M
MAXSIZE:  8.410M(2153), MAXRSS:  5.676M(1453)
AVGSIZE:  6.738M(1725), AVGRSS:  4.496M(1151)
$ ./proc-time.py ruby hash.rb
 
user:   7.84(784) system:   1.14(114)
elapsed:  9.468M
MAXSIZE: 13.727M(3514), MAXRSS: 12.129M(3105)
AVGSIZE: 12.539M(3210), AVGRSS: 10.879M(2785)

Tags: , , , ,

กำลังสงสั

กำลังสงสัยอยู่เลย ว่าคุณสุกรีจับค่าพวกนี้มาจากไหน ขอบคุณครับ

Post new comment