Skip to content

Instantly share code, notes, and snippets.

@jhansche
Created October 28, 2016 20:39
Show Gist options
  • Save jhansche/322965fa3c3a6179114bc3ac8e4337ec to your computer and use it in GitHub Desktop.
Save jhansche/322965fa3c3a6179114bc3ac8e4337ec to your computer and use it in GitHub Desktop.
Simple script to monitor the number open file descriptors for an Android application.
#!/bin/sh
# Usage: ./watch-fds.sh <application_id> [delay_secs = 5]
APP_ID=${1:?missing application id}
DELAY=$(( ${2:-5} ))
DEVICE_LIMIT=$(( $(adb shell ulimit -n) ))
WARN_THRESHOLD=$(( ${DEVICE_LIMIT} / 3 ))
echo "Will warn at ${WARN_THRESHOLD}"
APP_PID=$(( $(adb shell ps | grep "${APP_ID}" | awk '{print $2}') ))
echo "Watching application ${APP_ID} <${APP_PID}>. Press CTRL+C to stop."
fds=""
high_mark=0
while [ true ]; do
app_proc=$(( $(adb shell ps ${APP_PID} | grep -c ${APP_ID}) ))
if [ ${app_proc} -ne 1 ]; then
echo "Process ${APP_ID} <${APP_PID}> has died."
echo "Last list of File Descriptors:"
echo "${fds}"
exit
fi
fds="$(adb shell run-as ${APP_ID} ls -l /proc/${APP_PID}/fd 2>/dev/null)"
fd_count=$(( $(wc -l <<< "${fds}") ))
echo ${fd_count}
if [ ${fd_count} -ge ${WARN_THRESHOLD} -a ${fd_count} -gt ${high_mark} ]; then
echo " *** *** *** "
echo "Warning: FD count > warning threshold: ${fd_count}"
# echo "Current File Descriptors:"
# echo "${fds}"
echo " *** *** *** "
echo ""
# then ratchet the highwater mark
high_mark=$(( ${fd_count} ))
fi
sleep ${DELAY}
done
@jhansche
Copy link
Author

I don't even remember publishing this 😆

@AftabUfaq
Copy link

can you to this for an app running on windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment