Question: My bash script has a memory leak somewhere. How do I profile it?
Try: /usr/bin/time -v bash ./my_script.sh 2>&1 | grep -E 'Maximum|CPU|Elapsed|Voluntary'
Info: /usr/bin/time (GNU binary, not the shell builtin) with -v tracks detailed resource usage. Peak memory usage is shown under Maximum resident set size (Maximum RSS).
Examples:
- $
/usr/bin/time -v ./program 2>time.log# Log detailed resource statistics - $
/usr/bin/time -f '%M KB peak, %e sec, %P CPU' ./script.sh# Custom resource output - $
valgrind --tool=massif ./program# Memory profiling for compiled binaries
Note: The shell builtin time provides less detailed information, so /usr/bin/time is useful when you need GNU resource statistics. A high RSS value in a script can indicate that it is holding large files, arrays, or other data in memory. For a true memory-leak investigation, use a dedicated memory profiler such as Massif rather than relying on peak RSS alone.
Leave a Reply