[英]Comparing memory usage of shell script and Python script for same task
假定兩個腳本:(a)調用JAVA jar的bash shell腳本(以下稱為my_shell_script
),以及(b)從其他Python軟件包導入功能但不調用任何非Python軟件包或軟件的Python腳本(以下稱為my_Python_script
)。 這兩個腳本具有相同的目的:它們采用相同的輸入(以下稱為testinput
)並生成大致相同的輸出。
我想根據兩個腳本執行時的時間來衡量和比較它們的內存使用情況。
為此,我使用massif
(將time_unit設置為毫秒)通過valgrind
執行每個腳本,然后通過ms_print
總結了ms_print
輸出。
INF=testinput
# Testing Shell script
valgrind --tool=massif --pages-as-heap=yes --time-unit=ms --massif-out-file=${INF}_shell.out bash my_shell_script -f $INF -j my_java_jar
ms_print --threshold=50.0 ${INF}_shell.out > ${INF}_shell.summary
# Testing Python script
valgrind --tool=massif --pages-as-heap=yes --time-unit=ms --massif-out-file=${INF}_Python.out python2 my_python_script $INF
ms_print --threshold=50.0 ${INF}_Python.out > ${INF}_Python.summary
雖然valgrind / my_python_script
記錄的my_python_script
的內存使用情況與我通過htop
看到的大致一致,但my_shell_script
卻不是這樣。 htop
上的統計信息表明,在執行my_shell_script
過程中使用了1 GB的內存,而valgrind / my_shell_script
僅記錄了幾十MB的已使用內存。
因此,我懷疑valgrind / massif記錄了執行bash代碼的內存使用情況,但沒有記錄bash代碼正在調用的JAVA jar的內存使用情況。
如何正確測量my_shell_script
的內存使用量作為時間的函數?
選項--trace-children = yes | no表示跟蹤(或不跟蹤)子級。 默認值為no,這意味着執行腳本的shell將在valgrind下,但不在啟動的python或java進程下。
因此,指定--trace-children = yes。
確保在massif out文件參數中使用例如%p,否則所有進程(例如shell及其子進程)將寫入同一文件,這將無法正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.