Last active
January 2, 2019 20:51
-
-
Save xeoncross/66013ebcd7f51790c75db223fb33e471 to your computer and use it in GitHub Desktop.
Counting file descriptors used by a process or Go application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
func countOpenFiles() int { | |
out, err := exec.Command("/bin/sh", "-c", fmt.Sprintf("lsof -p %v | wc -l", os.Getpid())).Output() | |
if err != nil { | |
// log.Println("count files", err) | |
return 0 | |
} | |
n, err := strconv.Atoi(strings.TrimSpace(string(out))) | |
if err != nil { | |
return 0 | |
} | |
return n | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Process ID's using the most file descriptors | |
lsof | awk '{ print $2; }' | uniq -c | sort -rn | head | |
# Name of process + PID | |
sudo lsof | awk '{ print $1" "$2; }' | uniq -c | sort -rn | |
# OR | |
# Find the PID | |
ps aux | grep yourappname | |
# Count the file descriptors for this process | |
lsof -p PIDHERE | wc -l to count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://oroboro.com/file-handle-leaks-server/