# メモリ管理 (Go) > Stack と heap、escape analysis、garbage collector、メモリプロファイリング、pprof、メモリリーク - 24 面接問題 - Senior - [面接問題: Go](https://sharpskill.dev/ja/technologies/go/interview-questions.md) ## 1. Go における stack 割り当てと heap 割り当ての主な違いは何ですか? **回答** stack は自動的に管理され、割り当ては非常に高速です(単純な stack pointer の移動)。stack 上の変数は関数が return すると自動的に解放されます。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' は 'moved to heap: x' のようなメッセージを表示し、変数が heap に移動されたことを示します。このツールはクリティカルなコードでのメモリ割り当てを最適化するために不可欠です。 ## さらに21問利用可能 - ローカル変数が heap に escape するのはどのような場合ですか? - Go における garbage collector の主な役割は何ですか? 無料で登録: https://sharpskill.dev/ja/login ## その他のGo面接トピック - [Goの基礎](https://sharpskill.dev/ja/technologies/go/interview-questions/go-basics.md): 25問, Junior - [Goのデータ構造](https://sharpskill.dev/ja/technologies/go/interview-questions/go-data-structures.md): 20問, Junior - [Go のインターフェース](https://sharpskill.dev/ja/technologies/go/interview-questions/go-interfaces.md): 18問, Junior - [エラー処理](https://sharpskill.dev/ja/technologies/go/interview-questions/error-handling.md): 20問, Junior - [Goroutine の基礎](https://sharpskill.dev/ja/technologies/go/interview-questions/goroutines-basics.md): 20問, Junior - [Channels](https://sharpskill.dev/ja/technologies/go/interview-questions/channels.md): 22問, Junior - [Go Modules](https://sharpskill.dev/ja/technologies/go/interview-questions/go-modules.md): 18問, Mid-Level - [HTTP サーバー](https://sharpskill.dev/ja/technologies/go/interview-questions/http-server.md): 24問, Mid-Level - [HTTPクライアント](https://sharpskill.dev/ja/technologies/go/interview-questions/http-client.md): 18問, Mid-Level - [JSON エンコーディング](https://sharpskill.dev/ja/technologies/go/interview-questions/json-encoding.md): 20問, Mid-Level - [Database/SQL](https://sharpskill.dev/ja/technologies/go/interview-questions/database-sql.md): 22問, Mid-Level - [context パッケージ](https://sharpskill.dev/ja/technologies/go/interview-questions/context-package.md): 20問, Mid-Level - [Testing](https://sharpskill.dev/ja/technologies/go/interview-questions/testing.md): 22問, Mid-Level - [並行処理パターン](https://sharpskill.dev/ja/technologies/go/interview-questions/concurrency-patterns.md): 24問, Mid-Level - [同期プリミティブ](https://sharpskill.dev/ja/technologies/go/interview-questions/sync-primitives.md): 22問, Mid-Level - [Go Web フレームワーク](https://sharpskill.dev/ja/technologies/go/interview-questions/go-web-frameworks.md): 20問, Mid-Level - [REST API 設計](https://sharpskill.dev/ja/technologies/go/interview-questions/rest-api-design.md): 20問, Mid-Level - [gRPC](https://sharpskill.dev/ja/technologies/go/interview-questions/grpc.md): 22問, Mid-Level - [Reflection](https://sharpskill.dev/ja/technologies/go/interview-questions/reflection.md): 20問, Senior - [パフォーマンス最適化](https://sharpskill.dev/ja/technologies/go/interview-questions/performance-optimization.md): 22問, Senior - [Generics](https://sharpskill.dev/ja/technologies/go/interview-questions/generics.md): 20問, Senior - [Go Design Patterns](https://sharpskill.dev/ja/technologies/go/interview-questions/design-patterns.md): 24問, Senior - [Microservices](https://sharpskill.dev/ja/technologies/go/interview-questions/microservices.md): 24問, Senior - [セキュリティと認証](https://sharpskill.dev/ja/technologies/go/interview-questions/security-authentication.md): 24問, Senior - [Docker & Containerization](https://sharpskill.dev/ja/technologies/go/interview-questions/docker-containerization.md): 20問, Senior - [Kubernetes Basics](https://sharpskill.dev/ja/technologies/go/interview-questions/kubernetes-basics.md): 22問, Senior - [高度な Go](https://sharpskill.dev/ja/technologies/go/interview-questions/advanced-go.md): 20問, Senior - [CLI開発](https://sharpskill.dev/ja/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/ja/technologies/go/interview-questions/memory-management