Logging when limit Exceeds-Collection of common programming errors

if your issue is about memory allocation failed you could also use this script to monitor percentage of free memory left

#!/bin/bash
# you can adjust 0.20 meaning 20% to percentage of memory free you want 
output=$(free | grep "Swap" | awk '{if (($4/$2) < 0.20) print "memory low"}')
if [[ "$output" != "" ]]; then
    echo "..." | mail -s "Memory low on " 
fi

save it to script file (ex. memmon.sh) and add to crontab it will send email and write log file when detect free memory low as conditioned)

# Ex. monitor every 3 min.
*/3 * * * * /home/user/memmon.sh >> /home/user/memmon.log