比较同一任务的 shell 脚本和 Python 脚本的内存使用情况

Comparing memory usage of shell script and Python script for same task

假设有两个脚本:(a) bash shell 脚本调用 JAVA jar(以下简称 my_shell_script ),以及 (b) Python 脚本,它从其他 Python 包中导入函数,但不调用任何非 Python 包或软件(以下称为 my_Python_script)。这两个脚本具有相同的目的:它们采用相同的输入(以下简称 testinput)并生成大致相同的输出。

我想测量和比较两个脚本在执行时作为时间函数的内存使用情况。

为此,我使用 massif 通过 valgrind 执行每个脚本(将 time_unit 设置为毫秒),然后通过 [=18= 对地块输出进行总结].

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/massif 记录了 my_python_script 的内存使用情况,这与我通过 htop 看到的大致一致,但 my_shell_script 的情况并非如此。 htop 上的统计表明 my_shell_script 执行期间使用了 GB 的内存,而 valgrind/massif 只记录了几十 MB 的内存使用。

因此,我怀疑valgrind/massif记录了bash代码执行的内存使用情况,而不是bash代码所在的JAVAjar的内存使用情况打电话。

如何正确测量 my_shell_script 的内存使用量随时间的变化?

选项 --trace-children=yes|no 指示跟踪(或不跟踪)子项。默认值为 no,这意味着执行脚本的 shell 将 在 valgrind 下,但不是启动的 python 或 java 进程。

因此,指定 --trace-children=yes。

请务必使用例如massif 输出文件参数中的 %p,否则所有 进程(例如 shell 及其子进程)将写入同一个文件,这将无法正常工作。