Created
June 14, 2023 12:00
-
-
Save parsibox/ee4f1642fb95d1f77c2885694df94376 to your computer and use it in GitHub Desktop.
view all running Goroutines in golang
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
fmt.Println(runtime.NumGoroutine()) | |
// Get the stack traces of all currently running Goroutines. | |
stackTrace := make([]runtime.StackRecord, 1024) | |
length, ok := runtime.GoroutineProfile(stackTrace) | |
// Print out the stack traces. | |
fmt.Println("Stack traces of all Goroutines:") | |
for i := 0; i < length; i++ { | |
fmt.Println(stackTrace[i].Stack()) | |
} | |
// Check if profiling was successful. | |
if !ok { | |
fmt.Println("Profiling failed!") | |
} | |
stackTrace1 := make([]byte, 1024*1024) | |
length = runtime.Stack(stackTrace1, true) | |
// Print out the stack traces. | |
fmt.Println("Stack traces of all Goroutines:") | |
fmt.Println(string(stackTrace1[:length])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment