企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 语法 ``` func FreeOSMemory() // 强制进行一次垃圾收集 func PrintStack() func ReadGCStats(stats *GCStats) gc 的stats func SetGCPercent(percent int) int // SetMaxStack设置单个goroutine堆栈可以使用的最大内存量。 // 如果在扩展堆栈时有任何goroutine超出了此限制,程序将崩溃。 // 初始设置在64位系统上为1 GB,在32位系统上为250 MB。 func SetMaxStack(bytes int) int // 该限制控制操作系统线程数 func SetMaxThreads(threads int) int func SetPanicOnFault(enabled bool) bool func SetTraceback(level string) func Stack() []byte ``` ### type GCStats ``` type GCStats struct { LastGC time.Time // time of last collection NumGC int64 // number of garbage collections PauseTotal time.Duration // total pause for all collections Pause []time.Duration // pause history, most recent first PauseEnd []time.Time // pause end times history, most recent first PauseQuantiles []time.Duration } ``` ## 实例 ### debug.PrintStack 打印堆栈 ``` func main() { defer func() { if r := recover(); r != nil { err := r.(error) fmt.Printf("%+v\n", err) debug.PrintStack() } }() demo() time.Sleep(1 * time.Second) } func demo() { demo1() } func demo1() { panic("hello world") } ``` ### ReadGCStats ``` var a debug.GCStats debug.ReadGCStats(&a) fmt.Printf("%+v\n", a) ``` ### Stack ``` // 实现 func Stack() []byte { buf := make([]byte, 1024) for { n := runtime.Stack(buf, false) if n < len(buf) { return buf[:n] } buf = make([]byte, 2*len(buf)) } } ```