GO · 성능
pprof 메모리 프로파일링
힙 메모리 할당을 분석하여 메모리 사용량을 최적화합니다.
성능고급pprofmemoryheapgc
핵심 설명
힙 메모리 할당을 분석하여 메모리 사용량을 최적화합니다.
Go code
package main
import (
"fmt"
"os"
"runtime"
"runtime/pprof"
)
func allocateSlices() {
var data [][]byte
for i := 0; i < 1000; i++ {
buf := make([]byte, 10240) // 10KB
data = append(data, buf)
}
_ = data
}
func main() {
allocateSlices()
// 힙 프로파일 생성
f, _ := os.Create("mem.prof")
defer f.Close()
runtime.GC() // GC 실행 후 프로파일링
pprof.WriteHeapProfile(f)
// 메모리 통계
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Printf("힙 할당: %d MB\n", m.HeapAlloc/1024/1024)
fmt.Printf("시스템 메모리: %d MB\n", m.Sys/1024/1024)
fmt.Printf("GC 횟수: %d\n", m.NumGC)
fmt.Println("\n분석 명령어:")
fmt.Println(" go tool pprof mem.prof")
fmt.Println(" (pprof) top --alloc_space # 총 할당량 기준")
fmt.Println(" (pprof) top --inuse_space # 현재 사용 기준")
fmt.Println(" go tool pprof -http=:8080 mem.prof # 웹 UI")
os.Remove("mem.prof")
}학습 팁
--alloc_space는 총 할당량, --inuse_space는 현재 사용 중인 메모리를 보여줍니다. 목적에 따라 선택하세요.
주의할 점
프로파일 전 runtime.GC()를 호출하지 않으면 아직 GC되지 않은 객체가 포함되어 결과가 부정확합니다.
자주 묻는 질문
pprof 메모리 프로파일링란 무엇인가요?
힙 메모리 할당을 분석하여 메모리 사용량을 최적화합니다.
pprof 메모리 프로파일링 학습 시 주의할 점은 무엇인가요?
프로파일 전 runtime.GC() 를 호출하지 않으면 아직 GC되지 않은 객체가 포함되어 결과가 부정확합니다.
Continue Learning
Go 학습을 이어가세요
총 200개의 독립 HTML 학습 문서 중 하나입니다. 각 문서는 고유 URL과 canonical 메타데이터를 가집니다.