-
Notifications
You must be signed in to change notification settings - Fork 57
Bash sample code: Memory leak test script
Jean-Michel Gigault edited this page Dec 22, 2015
·
11 revisions
This is how to make a simple bash script that detects memory leak -- memory that the application has allocated, but has been lost and cannot be freed -- using any debugger tool that requires installation.
Requirement: The program from which you want to detect memory leak need to be running at least 15 consecutive seconds, enough time for it to generate memory leak (or not) and to let the bash script making its job at the same time.
function detect_memory_leaks
{
local PROG_PATH=$1 # 1st argument: The path to the executable to test
($PROG_PATH >/dev/null 2>&1) &
local PID=$!
sleep 5
if [ -z "$(ps a | awk -v PID=$PID '$1 == PID {print}')" ]
then
echo "An error occurred: The program has terminated too quickly"
return
fi
local RESULT=$(leaks --nocontext --nostacks $PID 2>/dev/null | grep 'total leaked bytes')
kill -9 $PID >/dev/null 2>&1
if [ -z "$RESULT" ]
then
echo "An error occurred: Unable to detect memory leak"
else
echo $RESULT
fi
}
detect_memory_leaks "./ft_ls -R /Users"Introduction:
- What is Bash
- What is a bash script
- What is 42FileChecker
- Contributing to 42FileChecker
Bash syntax:
Bash tools:
- Builtin commands
- Awk
- Cat
- Grep
- Sed
Bash sample codes:
- Script auto-update (git tool)
- Create an interactive menu
- Animated spinner with a time out
- Static var test
- Check the basic rules of a makefile
- Forbidden functions test
- Memory leak test script
- Create a speed test