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
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.
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().