SlideShare a Scribd company logo
Memory in Go
Jakarta, 16 November 2017
Meetup: GO-JEK and Go
iman.t@go-jek.com
About Me
● 7++ year code experience at various language including java, ruby, go, c++,
javascript, etc.
● 1++ year code in Go
● ~2 year on Gojek
● Fraud Engineer: working on fraud detection/prevention platform
Memory Management
"The memory management on the PowerPC can be used to
frighten small children." - Linus Torvalds
Address Space
Stack
Heap
Program Text
Free Memory
0x000000
0x7ffffff ● LIFO
● Last Allocate First Deallocate
● Cheaper and Faster
● Local variable and function parameter
● Free Store
● Free to allocate/deallocate
● Long life variable
Literally your code
A variable allocated on the heap or the stack?
● You don't need to know; it’s handle by compiler.
● When possible, variable will be allocate on the stack
● If variable is very large, allocate on the heap
● If variable is still referenced after the function returns, allocate to heap.
● Escape Analysis is a method to check whether variable escape to another
initial scope.
Escape Analysis
Because localVar
still referenced after
end of function
Pointers
“My address is like my shoes. It travel with me” - Mary
Harris Jones
Value and Reference
123
0xc42000a2a8
0
0xc42000a2a8
Value
Reference/Address
Operator &
Pointer is Data Type
Address of value
Nil Pointers is pointer that no
referencing any value
Operator *
Value of pointer
This is known as "dereferencing" or
"indirecting"
Pointers and Function
Everything in Go is
passed by value.
Function always gets a
copy of the thing being
passed
Slice/Map
Map and slice values
behave like pointers
Pointer Receiver
For big struct because
cheaper
For basic type, slice, or small
struct because efficient and clean
Not modify field on MyStruct
Be consistent
Garbage Collector
“C programmers think memory
management is too important to be
left to the computer. Lisp
programmers think memory
management is too important to be
left to the user.”
- Bjarne Stroustrup
“Go is building a garbage collector
(GC) not only for 2015 but for
2025”
- Richard Hudson
GC on General
● Garbage = memory occupied by objects that are no longer in use
● Mark - Sweep:
○ Mark – identifies which pieces of memory are in use and which are not
○ Sweep – removes objects identified during the “mark” phase
● GC Strategy:
○ Stop The World - completely halt execution of the program
○ Incremental - perform in discrete phases, with program execution
permitted between each phase
○ Concurrent - do not stop program execution at all, except perhaps
briefly when the program's execution stack is scanned
Why Go GC can probably better than Java?
Go Java
Thousands of Goroutines Tens of Java Threads
Synchronization via channels Synchronization via objects/locks
Runtime written in Go Runtime written in C
Control of spatial locality; Objects
can be embedded Interior pointers
(&foo.field)
Objects linked with pointers
GC on Go
● Prioritizing low latency and simplicity
● Concurrent, tri-color, mark-sweep collector
GC Latency on Go
● 300 ms (Go 1.4)
● 40 ms (Go 1.5)
● 3 ms (Go 1.6)
● ~1 ms (Go 1.7)
● < 100 μs (Go 1.8)
● < 100 μs even for very large heap (Go 1.9)
GC Optimization
● GOGC Variable = the ratio of freshly allocated data to live data remaining
after the previous collection reaches (percentage)
● The default is 100, mean triggered when total heap size is now 100% bigger
than (i.e., twice) the size of the reachable objects after the last collection
● If you want to lower the total time spent in GC, increase GOGC.
● If you want to trade more GC time for less memory, lower GOGC.
● GOGC=off disables the garbage collector entirely.
● You may trigger a garbage collection "manually" with runtime.GC().
Never Stop Learning
● https://golang.org/doc/faq
● https://talks.golang.org/2015/go-gc.pdf
● https://dave.cheney.net/2014/06/07/five-things-that-make-go-fast
● https://making.pusher.com/golangs-real-time-gc-in-theory-and-practice/
● https://dougrichardson.org/2016/01/23/go-memory-allocations.html
QUESTION?
We are hiring! Contact recruitment@go-jek.com
Shortcut contact: m.kautsar@gojek.com jessica.t@go-jek.com

More Related Content

PDF
Goroutines and Channels in practice
Guilherme Garnier
 
PDF
Concurrency in Golang
Oliver N
 
PDF
Go Concurrency
jgrahamc
 
PDF
Golang Channels
Joris Bonnefoy
 
PDF
Introduction to Kotlin coroutines
Roman Elizarov
 
PDF
Deep drive into rust programming language
Vigneshwer Dhinakaran
 
PPTX
Introduction to GoLang
NVISIA
 
PDF
Concurrency With Go
John-Alan Simmons
 
Goroutines and Channels in practice
Guilherme Garnier
 
Concurrency in Golang
Oliver N
 
Go Concurrency
jgrahamc
 
Golang Channels
Joris Bonnefoy
 
Introduction to Kotlin coroutines
Roman Elizarov
 
Deep drive into rust programming language
Vigneshwer Dhinakaran
 
Introduction to GoLang
NVISIA
 
Concurrency With Go
John-Alan Simmons
 

What's hot (20)

PDF
Introduction to Go programming language
Slawomir Dorzak
 
PDF
GDB Rocks!
Kent Chen
 
PPT
Introduction to gdb
Owen Hsu
 
PDF
Why rust?
Mats Kindahl
 
PPTX
Understand more about C
Yi-Hsiu Hsu
 
PDF
Memory Management with Page Folios
Adrian Huang
 
PDF
Channel in Go
Jaych Su
 
PDF
C++20 Key Features Summary
Chris Ohk
 
PDF
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
Johnny Sung
 
PDF
Interpreter, Compiler, JIT from scratch
National Cheng Kung University
 
PPTX
Go Programming Language (Golang)
Ishin Vin
 
PPTX
Golang - Overview of Go (golang) Language
Aniruddha Chakrabarti
 
PDF
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
National Cheng Kung University
 
PDF
BPF - in-kernel virtual machine
Alexei Starovoitov
 
PDF
淺談探索 Linux 系統設計之道
National Cheng Kung University
 
PDF
The Internals of "Hello World" Program
National Cheng Kung University
 
PPTX
Go Programming language, golang
Basil N G
 
PDF
New Ways to Find Latency in Linux Using Tracing
ScyllaDB
 
Introduction to Go programming language
Slawomir Dorzak
 
GDB Rocks!
Kent Chen
 
Introduction to gdb
Owen Hsu
 
Why rust?
Mats Kindahl
 
Understand more about C
Yi-Hsiu Hsu
 
Memory Management with Page Folios
Adrian Huang
 
Channel in Go
Jaych Su
 
C++20 Key Features Summary
Chris Ohk
 
[Golang] 以 Mobile App 工程師視角,帶你進入 Golang 的世界 (Introduction of GoLang)
Johnny Sung
 
Interpreter, Compiler, JIT from scratch
National Cheng Kung University
 
Go Programming Language (Golang)
Ishin Vin
 
Golang - Overview of Go (golang) Language
Aniruddha Chakrabarti
 
LLVM 總是打開你的心:從電玩模擬器看編譯器應用實例
National Cheng Kung University
 
BPF - in-kernel virtual machine
Alexei Starovoitov
 
淺談探索 Linux 系統設計之道
National Cheng Kung University
 
The Internals of "Hello World" Program
National Cheng Kung University
 
Go Programming language, golang
Basil N G
 
New Ways to Find Latency in Linux Using Tracing
ScyllaDB
 
Ad

Similar to Memory in go (20)

PDF
Advanced memory allocation
Joris Bonnefoy
 
PPTX
golang_getting_started.pptx
Guy Komari
 
PDF
Go and the garbage collection
Joris Bonnefoy
 
PDF
Golang and Eco-System Introduction / Overview
Markus Schneider
 
PDF
Move from C to Go
Yu-Shuan Hsieh
 
PPTX
The Go Programing Language 1
İbrahim Kürce
 
PPTX
Should i Go there
Shimi Bandiel
 
PDF
Memory Management in Go: Stack, Heap & Garbage Collector
Wednesday Solutions
 
PPTX
Golang
Fatih Şimşek
 
PDF
Go Lang Tutorial
Wei-Ning Huang
 
PDF
Golang online course
bestonlinecoursescoupon
 
PDF
Learning Go Programming 1st Edition Vladimir Vivien
latasdugatk8
 
ODP
Ready to go
Atin Mukherjee
 
PPTX
Lab2_AdvancedGoConceptswithgo_foundationofgolang_.pptx
stasneemattia
 
PDF
Pointers & functions
Manjitsing Valvi
 
PPT
Go1
vivekraj3434
 
PPT
GO programming language
tung vu
 
PDF
golang_refcard.pdf
Spam92
 
PPT
Introduction to Go ProgrammingLanguage.ppt
PedroAlexandre215482
 
PDF
Golang
Saray Chak
 
Advanced memory allocation
Joris Bonnefoy
 
golang_getting_started.pptx
Guy Komari
 
Go and the garbage collection
Joris Bonnefoy
 
Golang and Eco-System Introduction / Overview
Markus Schneider
 
Move from C to Go
Yu-Shuan Hsieh
 
The Go Programing Language 1
İbrahim Kürce
 
Should i Go there
Shimi Bandiel
 
Memory Management in Go: Stack, Heap & Garbage Collector
Wednesday Solutions
 
Go Lang Tutorial
Wei-Ning Huang
 
Golang online course
bestonlinecoursescoupon
 
Learning Go Programming 1st Edition Vladimir Vivien
latasdugatk8
 
Ready to go
Atin Mukherjee
 
Lab2_AdvancedGoConceptswithgo_foundationofgolang_.pptx
stasneemattia
 
Pointers & functions
Manjitsing Valvi
 
GO programming language
tung vu
 
golang_refcard.pdf
Spam92
 
Introduction to Go ProgrammingLanguage.ppt
PedroAlexandre215482
 
Golang
Saray Chak
 
Ad

Recently uploaded (20)

PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
The Future of Artificial Intelligence (AI)
Mukul
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
Doc9.....................................
SofiaCollazos
 
OFFOFFBOX™ – A New Era for African Film | Startup Presentation
ambaicciwalkerbrian
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 

Memory in go

  • 1. Memory in Go Jakarta, 16 November 2017 Meetup: GO-JEK and Go [email protected]
  • 2. About Me ● 7++ year code experience at various language including java, ruby, go, c++, javascript, etc. ● 1++ year code in Go ● ~2 year on Gojek ● Fraud Engineer: working on fraud detection/prevention platform
  • 3. Memory Management "The memory management on the PowerPC can be used to frighten small children." - Linus Torvalds
  • 4. Address Space Stack Heap Program Text Free Memory 0x000000 0x7ffffff ● LIFO ● Last Allocate First Deallocate ● Cheaper and Faster ● Local variable and function parameter ● Free Store ● Free to allocate/deallocate ● Long life variable Literally your code
  • 5. A variable allocated on the heap or the stack? ● You don't need to know; it’s handle by compiler. ● When possible, variable will be allocate on the stack ● If variable is very large, allocate on the heap ● If variable is still referenced after the function returns, allocate to heap. ● Escape Analysis is a method to check whether variable escape to another initial scope.
  • 6. Escape Analysis Because localVar still referenced after end of function
  • 7. Pointers “My address is like my shoes. It travel with me” - Mary Harris Jones
  • 9. Operator & Pointer is Data Type Address of value Nil Pointers is pointer that no referencing any value
  • 10. Operator * Value of pointer This is known as "dereferencing" or "indirecting"
  • 11. Pointers and Function Everything in Go is passed by value. Function always gets a copy of the thing being passed
  • 12. Slice/Map Map and slice values behave like pointers
  • 13. Pointer Receiver For big struct because cheaper For basic type, slice, or small struct because efficient and clean Not modify field on MyStruct Be consistent
  • 14. Garbage Collector “C programmers think memory management is too important to be left to the computer. Lisp programmers think memory management is too important to be left to the user.” - Bjarne Stroustrup “Go is building a garbage collector (GC) not only for 2015 but for 2025” - Richard Hudson
  • 15. GC on General ● Garbage = memory occupied by objects that are no longer in use ● Mark - Sweep: ○ Mark – identifies which pieces of memory are in use and which are not ○ Sweep – removes objects identified during the “mark” phase ● GC Strategy: ○ Stop The World - completely halt execution of the program ○ Incremental - perform in discrete phases, with program execution permitted between each phase ○ Concurrent - do not stop program execution at all, except perhaps briefly when the program's execution stack is scanned
  • 16. Why Go GC can probably better than Java? Go Java Thousands of Goroutines Tens of Java Threads Synchronization via channels Synchronization via objects/locks Runtime written in Go Runtime written in C Control of spatial locality; Objects can be embedded Interior pointers (&foo.field) Objects linked with pointers
  • 17. GC on Go ● Prioritizing low latency and simplicity ● Concurrent, tri-color, mark-sweep collector
  • 18. GC Latency on Go ● 300 ms (Go 1.4) ● 40 ms (Go 1.5) ● 3 ms (Go 1.6) ● ~1 ms (Go 1.7) ● < 100 μs (Go 1.8) ● < 100 μs even for very large heap (Go 1.9)
  • 19. GC Optimization ● GOGC Variable = the ratio of freshly allocated data to live data remaining after the previous collection reaches (percentage) ● The default is 100, mean triggered when total heap size is now 100% bigger than (i.e., twice) the size of the reachable objects after the last collection ● If you want to lower the total time spent in GC, increase GOGC. ● If you want to trade more GC time for less memory, lower GOGC. ● GOGC=off disables the garbage collector entirely. ● You may trigger a garbage collection "manually" with runtime.GC().
  • 20. Never Stop Learning ● https://golang.org/doc/faq ● https://talks.golang.org/2015/go-gc.pdf ● https://dave.cheney.net/2014/06/07/five-things-that-make-go-fast ● https://making.pusher.com/golangs-real-time-gc-in-theory-and-practice/ ● https://dougrichardson.org/2016/01/23/go-memory-allocations.html