Memory in Go
Jakarta, 16 November 2017
Meetup: GO-JEK and Go
● 7++ year code experience at various language including java, ruby, go, c++,
● 1++ year code in Go
● ~2 year on Gojek
● Fraud Engineer: working on fraud detection/prevention platform
"The memory management on the PowerPC can be used to
frighten small children." - Linus Torvalds
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
still referenced after
end of function
“My address is like my shoes. It travel with me” - Mary
Value and Reference
Pointer is Data Type
Address of value
Nil Pointers is pointer that no
referencing any value
Value of pointer
This is known as "dereferencing" or
Pointers and Function
Everything in Go is
passed by value.
Function always gets a
copy of the thing being
Map and slice values
behave like pointers
For big struct because
For basic type, slice, or small
struct because efficient and clean
Not modify field on MyStruct
“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
- 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?
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
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)
● 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().
Parece que tem um bloqueador de anúncios ativo. Ao listar o SlideShare no seu bloqueador de anúncios, está a apoiar a nossa comunidade de criadores de conteúdo.
Atualizámos a nossa política de privacidade.
Atualizámos a nossa política de privacidade de modo a estarmos em conformidade com os regulamentos de privacidade em constante mutação a nível mundial e para lhe fornecer uma visão sobre as formas limitadas de utilização dos seus dados.
Pode ler os detalhes abaixo. Ao aceitar, está a concordar com a política de privacidade atualizada.