直接回答:标准链路:服务暴露 net/http/pprof(import 即注册 /debug/pprof/);CPU 用 go tool pprof http://host/debug/pprof/profile?seconds=30 采 30 秒火焰图,按采样占比定位热点函数;内存用 /debug/pprof/heap 看 inuse(当前占用)与 alloc(累计分配),--inuse_space 找泄漏、--alloc_space 找分配热点;goroutine、block、mutex profile 分别查泄漏、阻塞、锁竞争。top、list 函数名、web 出图是最常用三板斧。
展开解析:方法论比工具重要:先确定症状(CPU 高、内存涨、延迟毛刺),再选对应 profile。CPU 热点要区分"业务计算"与"运行时开销"——GC(runtime.mallocgc)、序列化、map 扩容常占大头。内存分析注意 heap profile 是采样(默认每 512KB 一次),小对象多的场景要按 alloc_objects 看。线上纪律:pprof 端口绝不能公网暴露(信息泄漏加 DoS 面),放内网或加鉴权;CPU profile 采集有开销,30 秒足矣;生产建议接 continuous profiling(pyroscope、parca)保留历史现场,事后救火不必复现。两个辅助:trace(runtime/trace)看调度延迟与 STW;-benchmem 在基准阶段就盯住分配。
追问方向:heap profile 的采样机制对结论有何影响?goroutine profile 中怎么识别泄漏簇?block profile 需要先设置什么?
(约 440 字)