Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
GoのGC (garbage collector)について理解する
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
uji
April 22, 2022
Technology
8.2k
11
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
GoのGC (garbage collector)について理解する
uji
April 22, 2022
More Decks by uji
See All by uji
【Go ハンズオン】Windows / Linux コマンドを Go で再現
uji
1
130
testdata ディレクトリを活用しよう!
uji
1
480
MCP 公式の Go SDK で QA データ登録アシスタントを作ってみた
uji
0
180
Green Tea GCの動作を紙芝居にしてみる
uji
1
220
Go Proverbs の紹介
uji
2
230
Go標準の暗号ライブラリメンテナンス戦略
uji
3
780
NOT A HOTEL TECH TALK ーSOFTWARE 3.0への道筋ー NEXT Web3 (2024-08-07)
uji
0
270
マネーフォワード ME ブロックチェーン連携機能におけるGoの活用方法
uji
1
330
Goはブロックチェーン領域でなぜ使われ、どのように活躍しているのか
uji
1
830
Other Decks in Technology
See All in Technology
Screen Lens - 今見てる画面を翻訳する
komagata
0
320
AIに任せた品質は、誰が見立てるのか - AI時代のテストマネジメント
nakanao
2
1.9k
2026_devsumi_ozono.pdf
o3
3
510
人間はどの意思決定を手放せるのか
kawasima
15
7.1k
目の前の楽しいが人生を変える - コミュニティの螺旋の歩き方と楽しむコツ / change your life
soudai
PRO
5
630
研究開発部の紹介 / Sansan R&D Profile
sansan33
PRO
5
25k
AI時代、データエンジニアが一番おもろい
genshun9
0
640
俺の仕事は AIに奪われないし、たぶんその BIも要らない
hikaruri
0
500
積み重なった技術負債への挑戦 〜初手としての全社ゴト化〜
techtekt
PRO
0
1.3k
幾何アルゴリズムで なめらかなピン操作を / iOSDC Japan 2026 / smoothpin
kazumanagano
0
350
負債のメタファと2026年 / Debt Metaphor in Agentic Engineering Age 202609 Edition
twada
PRO
8
3.8k
OpenTelemetry eBPF Instrumentationの舞台裏 / Behind the Scenes of OpenTelemetry eBPF Instrumentation
ymotongpoo
4
1.3k
Featured
See All Featured
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
660
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
520
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.1k
エンジニアに許された特別な時間の終わり
watany
109
250k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
420
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
42k
ラッコキーワード サービス紹介資料
rakko
1
4.9M
[SF Ruby Conf 2025] Rails X
palkan
3
1.4k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
500
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
990
Transcript
スライドトップと してご利用ください マネーフォワード事業本部 山田 太郎 © Money Forward, Inc.
GoのGC について理解する Understanding Go's GC Money Forward, Inc. uji © Money Forward, Inc.
@uji_rb • Software engineer @Money Forward, Inc.(Osaka) • A
three-year-old Gopher. • Kyoto.go © Money Forward, Inc. uji
Today’s goal • GCの目的を理解してもらう Help them understand the purpose of
GC. • GoのGCの仕組み、設計思想をざっくり理解してもらう Get a rough understanding of Go's GC structure and design philosophy. • Goのランタイムに興味を持ってもらう Get people interested in Go's runtime. © Money Forward, Inc.
© Money Forward, Inc. Agenda • GCとは? What is the
GC? • GoのGCの特長 Features of Go's GC. • GoのGCの進化の軌跡 The evolutionary path of Go's GC.
© Money Forward, Inc. GCとは? What is the GC?
© Money Forward, Inc. What is the GC? Garbage
Collector or Garbage Collection
© Money Forward, Inc. メモリ上のごみを見つけて回収し プログラムが再利用できるようにしてくれる Finds and collects
garbage in memory and enables to reuse from programs. What is the GC?
煩わしい手動メモリ管理からの解放される You can get rid of manual memory management.
より本質的なプログラミングに集中できる You can focus on more essential programming. © Money Forward, Inc. With GC
使われなくなったメモリ領域を 手動で解放しなければならない We must release unused memory space
manually. メモリリークや脆弱性などが発生しないように プログラムを書く必要がある It could lead to memory leaks and vulnerabilities. © Money Forward, Inc. The world without GC
→ No GC動作中にアプリの完全停止が発生 During GC operation, Complete stoppage
of the application occurs. © Money Forward, Inc. GCがあると良い事ばかり? Is it all good to have a GC?
→ No GC動作中にアプリの完全停止が発生 During GC operation, Complete stoppage
of the application occurs. Stop The World (STW) © Money Forward, Inc. GCがあると良い事ばかり? Is it all good to have a GC?
© Money Forward, Inc. 2分毎にパフォーマンスが悪化 Performance deteriorates every 2 minutes
Case study (Discord) https://discord.com/blog/why-discord-is-switching-from-go-to-rust
© Money Forward, Inc. 2分毎にパフォーマンスが悪化 Performance deteriorates every 2 minutes
Case study (Discord) https://discord.com/blog/why-discord-is-switching-from-go-to-rust Go 1.9.2 バージョンが古い Outdated version
サービスが常に低レイテンシを求める場合STW の影響を考慮する必要がある If the service always requires low latency,
we need to consider the impact of STW. © Money Forward, Inc. Stop The World
Goはプロダクト開発をより生産的に するために生まれた言語 Go was designed and developed to make product
development more productive. 本質的なプログラミングに集中させる思想がGoの 哲学と一致した The idea of focusing on essential programming coincided with Go's philosophy. © Money Forward, Inc. Why did Go choose GC? https://talks.golang.org/2012/splash.article
© Money Forward, Inc. GoのGCの特長 Features of Go's GC.
GCにおいて、 スループットとレイテンシはトレードオフ In GC, the Throughput and latency are
a trade-off. Goはレイテンシに重きをおいている Go is latency oriented. © Money Forward, Inc.
スループットは ハードウェアの進化によって向上していく Throughput increases with hardware evolution. ハードウェアで解決しづらいレイテンシを重視 Focus
on latency, which is difficult to solve with hardware © Money Forward, Inc. The Go Blog "Go GC: Prioritizing low latency and simplicity" Why does Go focus on latency?
• チューニングパラメータは”GOGC”のみ Tuning parameter is "GOGC" only. • 非同期処理はGoユーザーと同様にGoroutineを利用
Asynchronous processing uses Goroutine just like Go users. • アルゴリズムがシンプル Algorithm is simple. © Money Forward, Inc. 簡潔性を重視 Emphasize simplicity.
None
© Money Forward, Inc. We are hiring! Money Forwardは Goを書きたいエンジニアを
募集しています! (Go以外も書けます!) 求人一覧 (HRMOS)
“Concurrent Mark Sweep” (CMS) 最古のGCアルゴリズム “Mark Sweep” を改良したもの
It is an improved version of the oldest GC algorithm "Mark Sweep". © Money Forward, Inc. Go's GC algorithm
Mark Sweep アプリケーションで使われている メモリオブジェクトをMark Mark memory objects used in the
application ↓ MarkされていないオブジェクトをSweep Sweep unmarked objects © Money Forward, Inc.
© Money Forward, Inc.
© Money Forward, Inc. Target
Mark phase © Money Forward, Inc.
Sweep phase © Money Forward, Inc.
Sweep phase © Money Forward, Inc.
Mark Sweepの問題 Mark Sweep's problems. GCが動き続けている間 ずっとアプリケーションが止まり レイテンシが高くなる While
the GC keeps moving. The application stops all the time and latency is high. © Money Forward, Inc.
Mark Sweep とアプリケーションを 同時に動作させる Run Mark Sweep and application
in the same time. © Money Forward, Inc. Concurrent Mark Sweep (CMS) White 未探索 unsearched Gray 探索中 sarching Black 探索済み sharched Use tri-color marking
1. Sweep termination 2. Mark 3. Mark termination 4. Sweep
© Money Forward, Inc. Phases https://github.com/golang/go/blob/master/src/runtime/mgc.go
Sweep termination phase © Money Forward, Inc. Application STW 前回のGCループを止めて
消し残りを消しきる Erase the remainder of the previous loop. ↓ オブジェクトを全て白にする Mark all objects white.
Mark phase © Money Forward, Inc. Application
Mark phase © Money Forward, Inc. Application Queuing
Mark terminate phase © Money Forward, Inc. Application STW
Sweep phase © Money Forward, Inc. Application
オブジェクトの参照が切り替わったときに 色の塗り忘れが発生する場合がある When object references are switched Color forgetting
occurs. © Money Forward, Inc. 同時実行の問題 Concurrency problems.
© Money Forward, Inc. Example Allocation Forgetting to mark
© Money Forward, Inc. Example Changing point. Forgetting to mark
スループットの低下があるが アプリケーションを同時に動かせる Throughput is reduced, but applications can run concurrency.
© Money Forward, Inc. 新しく確保されたオブジェクトは黒に Mark the newly allocated object black. 親からの参照が切り替わったオブジェクトは灰色に Mark objects that have switched references from their parents gray. Write Barrier
1. Sweep termination 2. Mark 3. Mark termination 4. Sweep
© Money Forward, Inc. Reduced STW timing STW STW
GoのGCはCMS + Write Barrierで 低レイテンシを実現している Go's GC with CMS +
Write Barrier Low latency is achieved. コンパクションや世代別GCなどは 採用されていない No compaction or generational GC is employed. © Money Forward, Inc.
なぜこのようなシンプルなアルゴリズムで低レイテンシを実現で きているのか? Why is it possible to achieve low latency
with such a simple algorithm? • 値型の存在や高性能なエスケープ解析により、スタックが効率よく使 われるのでGCの負担が少ない The presence of value types and high-performance escape analysis make efficient use of the stack and reduce the burden on the GC. • TCMallocベースのメモリアロケーターが断片化を起こりづらい TCMalloc-based memory allocator is less prone to fragmentation. © Money Forward, Inc. ・Why golang garbage-collector not implement Generational and Compact gc? ・ Go Does Not Need a Java Style GC
© Money Forward, Inc. GoのGCの進化の軌跡 The evolutionary path of Go's
GC.
~ Go 1.2 Mark Sweep Go 1.3 Concurrent Sweep Go
1.5 CMS, Write Barrier © Money Forward, Inc. ~ Go 1.5
© Money Forward, Inc. Go 1.5 Go GC: Prioritizing Low
Latency and Simplicity
© Money Forward, Inc. Go 1.6 Go GC: Prioritizing Low
Latency and Simplicity ヒープサイズが大きい場合で も低レイテンシを維持 Low latency can be maintained even with large heap sizes.
Go 1.8 スタック再スキャンによるSTWを改善 Eliminate STW due to stack rescanning.(proposal) Go
1.10 アロケーションのレイテンシと全体的なオーバヘッド削減 Reduce allocation latency and overall overhead. Go 1.12 Go1.8から対応していたスタック再スキャンによるSTW解消 STW resolution by stack rescanning, supported since Go 1.8 Go 1.13 Scavengingをよりスマートに Smarter Scavenging.(issue) Go 1.14 goroutineがプリエンプティブになりGCの遅延削減 Goroutine becomes preemptive and reduces GC delay Go 1.15 GC動作中にReadMemStatsがブロックされる問題解消 Fixed ReadMemStats blocking during GC operation. © Money Forward, Inc. Release History
GC Pacerが改善された GC Pacer has been improved. GCの頻度を決める際、 ヒープ以外(スタック等)も見るようになった
When deciding how often to GC, added to look at other than heap (stack, etc.). GCの処理時間が減るかも Might reduce GC processing time. © Money Forward, Inc. Go 1.18 (released 2022/03) proposal "GC Pacer Redesign"
© Money Forward, Inc. SLOs The Go Blog "Getting to
Go: The Journey of Go's Garbage Collector"
改善の余地はまだまだある There is still room for improvement. どこまで進化するのかとても楽しみ I'm
very excited to see how far it will evolve. © Money Forward, Inc. Future of Go's GC
Summary • GCはプログラミングの難易度を下げ、 プロダクト開発の本質に集中させてくれる GC makes programming easy and helps
us focus on the essence of product development. • GoのGCは低レイテンシを重視しており、 それをシンプルなアルゴリズムで実現している Go's GC emphasizes low latency and it is achieved with a simple algorithm. • GoのGCはこれからも進化する Go's GC will continue to evolve. © Money Forward, Inc.
今回のセッションをきっかけに GoやGC、ランタイムについて 興味を持ってもらえると嬉しいです I hope this session will spark your
interest in Go, GC, and runtime. ご清聴ありがとうございました Thank you for your attention. © Money Forward, Inc.
Bibliography • ガベージコレクションのアルゴリズムと実装 中村 成洋, 相川 光, 竹内 郁雄(監修) https://tatsu-zine.com/books/gcbook
• The Go Blog ◦ Getting to Go: The Journey of Go's Garbage Collector https://go.dev/blog/ismmkeynote ◦ Go GC: Prioritizing low latency and simplicity https://go.dev/blog/go15gc ◦ Go's New Brand https://go.dev/blog/go-brand • Go GC: Latency Problem Solved https://talks.golang.org/2015/go-gc.pdf • go gc algorithm 101https://speakerdeck.com/taxio/go-gc-algorithm-101 • Go言語のGCについてhttps://engineering.linecorp.com/ja/blog/go-gc • GolangのGCを追う https://deeeet.com/writing/2016/05/08/gogc-2016 • GoはいつGCするのか? https://zenn.dev/koron/articles/b96cccfa82c0c1 • Dumpster diving the Go garbage collector https://blog.px.dev/go-garbage-collector/ • Go言語のリアルタイムGC 理論と実践 https://postd.cc/golangs-real-time-gc-in-theory-and-practice Illustrations ©tottie / Renée French https://github.com/tottie000/GopherIllustrations © Money Forward, Inc.