Successfully reported this slideshow.
Your SlideShare is downloading. ×

Memory in go

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Upcoming SlideShare
Reliability at scale
Reliability at scale
Loading in …3
×

Check these out next

1 of 21 Ad
Advertisement

More Related Content

Slideshows for you (20)

Similar to Memory in go (20)

Advertisement

Recently uploaded (20)

Memory in go

  1. 1. Memory in Go Jakarta, 16 November 2017 Meetup: GO-JEK and Go iman.t@go-jek.com
  2. 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. 3. Memory Management "The memory management on the PowerPC can be used to frighten small children." - Linus Torvalds
  4. 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. 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. 6. Escape Analysis Because localVar still referenced after end of function
  7. 7. Pointers “My address is like my shoes. It travel with me” - Mary Harris Jones
  8. 8. Value and Reference 123 0xc42000a2a8 0 0xc42000a2a8 Value Reference/Address
  9. 9. Operator & Pointer is Data Type Address of value Nil Pointers is pointer that no referencing any value
  10. 10. Operator * Value of pointer This is known as "dereferencing" or "indirecting"
  11. 11. Pointers and Function Everything in Go is passed by value. Function always gets a copy of the thing being passed
  12. 12. Slice/Map Map and slice values behave like pointers
  13. 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. 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. 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. 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. 17. GC on Go ● Prioritizing low latency and simplicity ● Concurrent, tri-color, mark-sweep collector
  18. 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. 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. 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
  21. 21. QUESTION? We are hiring! Contact recruitment@go-jek.com Shortcut contact: m.kautsar@gojek.com jessica.t@go-jek.com

×