# 메모리 관리 (Go) > Stack vs heap, escape analysis, garbage collector, 메모리 프로파일링, pprof, 메모리 누수 - 24 면접 질문 - Senior - [면접 질문: Go](https://sharpskill.dev/ko/technologies/go/interview-questions.md) ## 1. Go에서 stack 할당과 heap 할당의 주요 차이점은 무엇입니까? **답변** stack은 자동으로 관리되며 할당이 매우 빠릅니다(단순한 stack pointer 이동). stack의 변수는 함수가 반환될 때 자동으로 해제됩니다. heap은 메모리를 해제하기 위해 garbage collector가 필요하며, Go runtime이 관여하기 때문에 할당이 더 느립니다. 함수를 넘어 살아남는 변수나 컴파일 시점에 크기를 알 수 없는 변수는 heap으로 갑니다. ## 2. Go에서 escape analysis란 무엇입니까? **답변** escape analysis는 변수가 stack에 머무를 수 있는지 또는 heap에 할당되어야 하는지를 판단하기 위해 Go 컴파일러가 수행하는 정적 분석입니다. 컴파일러가 변수가 생성 함수 외부에서 참조된다고 감지하면(반환된 포인터, 전역 변수에 저장 등) 변수는 'escape'하여 heap에 할당됩니다. 이 분석은 메모리 할당을 자동으로 최적화합니다. ## 3. 컴파일 시 escape analysis 결과를 확인하려면 어떻게 합니까? **답변** -gcflags=-m 플래그를 사용하면 escape analysis 결정을 확인할 수 있습니다. -m=2 또는 -m=3을 사용하면 변수가 escape하는 이유에 대한 더 자세한 정보를 얻을 수 있습니다. 예를 들어 'go build -gcflags=-m=2'는 변수가 heap으로 이동되었음을 나타내는 'moved to heap: x'와 같은 메시지를 표시합니다. 이 도구는 중요한 코드에서 메모리 할당을 최적화하는 데 필수적입니다. ## 21개 추가 질문 이용 가능 - 지역 변수가 heap으로 escape하는 경우는 언제입니까? - Go에서 garbage collector의 주요 역할은 무엇입니까? 무료로 가입하기: https://sharpskill.dev/ko/login ## 기타 Go 면접 주제 - [Go 기초](https://sharpskill.dev/ko/technologies/go/interview-questions/go-basics.md): 25개 질문, Junior - [Go 데이터 구조](https://sharpskill.dev/ko/technologies/go/interview-questions/go-data-structures.md): 20개 질문, Junior - [Go 인터페이스](https://sharpskill.dev/ko/technologies/go/interview-questions/go-interfaces.md): 18개 질문, Junior - [에러 처리](https://sharpskill.dev/ko/technologies/go/interview-questions/error-handling.md): 20개 질문, Junior - [Goroutine 기초](https://sharpskill.dev/ko/technologies/go/interview-questions/goroutines-basics.md): 20개 질문, Junior - [Channels](https://sharpskill.dev/ko/technologies/go/interview-questions/channels.md): 22개 질문, Junior - [Go Modules](https://sharpskill.dev/ko/technologies/go/interview-questions/go-modules.md): 18개 질문, Mid-Level - [HTTP 서버](https://sharpskill.dev/ko/technologies/go/interview-questions/http-server.md): 24개 질문, Mid-Level - [HTTP 클라이언트](https://sharpskill.dev/ko/technologies/go/interview-questions/http-client.md): 18개 질문, Mid-Level - [JSON 인코딩](https://sharpskill.dev/ko/technologies/go/interview-questions/json-encoding.md): 20개 질문, Mid-Level - [Database/SQL](https://sharpskill.dev/ko/technologies/go/interview-questions/database-sql.md): 22개 질문, Mid-Level - [context 패키지](https://sharpskill.dev/ko/technologies/go/interview-questions/context-package.md): 20개 질문, Mid-Level - [Testing](https://sharpskill.dev/ko/technologies/go/interview-questions/testing.md): 22개 질문, Mid-Level - [동시성 패턴](https://sharpskill.dev/ko/technologies/go/interview-questions/concurrency-patterns.md): 24개 질문, Mid-Level - [동기화 프리미티브](https://sharpskill.dev/ko/technologies/go/interview-questions/sync-primitives.md): 22개 질문, Mid-Level - [Go 웹 프레임워크](https://sharpskill.dev/ko/technologies/go/interview-questions/go-web-frameworks.md): 20개 질문, Mid-Level - [REST API 설계](https://sharpskill.dev/ko/technologies/go/interview-questions/rest-api-design.md): 20개 질문, Mid-Level - [gRPC](https://sharpskill.dev/ko/technologies/go/interview-questions/grpc.md): 22개 질문, Mid-Level - [Reflection](https://sharpskill.dev/ko/technologies/go/interview-questions/reflection.md): 20개 질문, Senior - [성능 최적화](https://sharpskill.dev/ko/technologies/go/interview-questions/performance-optimization.md): 22개 질문, Senior - [Generics](https://sharpskill.dev/ko/technologies/go/interview-questions/generics.md): 20개 질문, Senior - [Go Design Patterns](https://sharpskill.dev/ko/technologies/go/interview-questions/design-patterns.md): 24개 질문, Senior - [Microservices](https://sharpskill.dev/ko/technologies/go/interview-questions/microservices.md): 24개 질문, Senior - [보안 및 인증](https://sharpskill.dev/ko/technologies/go/interview-questions/security-authentication.md): 24개 질문, Senior - [Docker & Containerization](https://sharpskill.dev/ko/technologies/go/interview-questions/docker-containerization.md): 20개 질문, Senior - [Kubernetes Basics](https://sharpskill.dev/ko/technologies/go/interview-questions/kubernetes-basics.md): 22개 질문, Senior - [고급 Go](https://sharpskill.dev/ko/technologies/go/interview-questions/advanced-go.md): 20개 질문, Senior - [CLI 개발](https://sharpskill.dev/ko/technologies/go/interview-questions/cli-development.md): 20개 질문, Senior --- Source: SharpSkill (https://sharpskill.dev), tech interview preparation for your real stack. HTML version of this page: https://sharpskill.dev/ko/technologies/go/interview-questions/memory-management