SlideShare a Scribd company logo
1 of 21
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

What's hot

What's hot (20)

Golang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / OverviewGolang and Eco-System Introduction / Overview
Golang and Eco-System Introduction / Overview
 
GoLang Introduction
GoLang IntroductionGoLang Introduction
GoLang Introduction
 
Go Concurrency
Go ConcurrencyGo Concurrency
Go Concurrency
 
Coding with golang
Coding with golangCoding with golang
Coding with golang
 
Golang Channels
Golang ChannelsGolang Channels
Golang Channels
 
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...Choisir entre une API  RPC, SOAP, REST, GraphQL?  
Et si le problème était ai...
Choisir entre une API RPC, SOAP, REST, GraphQL? 
Et si le problème était ai...
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
 
Introduction to Go language
Introduction to Go languageIntroduction to Go language
Introduction to Go language
 
The Go programming language - Intro by MyLittleAdventure
The Go programming language - Intro by MyLittleAdventureThe Go programming language - Intro by MyLittleAdventure
The Go programming language - Intro by MyLittleAdventure
 
Introduction to go lang
Introduction to go langIntroduction to go lang
Introduction to go lang
 
Go Programming Language (Golang)
Go Programming Language (Golang)Go Programming Language (Golang)
Go Programming Language (Golang)
 
Golang
GolangGolang
Golang
 
Go language presentation
Go language presentationGo language presentation
Go language presentation
 
Rust system programming language
Rust system programming languageRust system programming language
Rust system programming language
 
Google mock training
Google mock trainingGoogle mock training
Google mock training
 
Golang - Overview of Go (golang) Language
Golang - Overview of Go (golang) LanguageGolang - Overview of Go (golang) Language
Golang - Overview of Go (golang) Language
 
Go Lang Tutorial
Go Lang TutorialGo Lang Tutorial
Go Lang Tutorial
 
Why you should care about Go (Golang)
Why you should care about Go (Golang)Why you should care about Go (Golang)
Why you should care about Go (Golang)
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Rust Programming Language
Rust Programming LanguageRust Programming Language
Rust Programming Language
 

Similar to Memory in go

Aws uk ug #8 not everything that happens in vegas stay in vegas
Aws uk ug #8   not everything that happens in vegas stay in vegasAws uk ug #8   not everything that happens in vegas stay in vegas
Aws uk ug #8 not everything that happens in vegas stay in vegas
Peter Mounce
 

Similar to Memory in go (20)

Advanced memory allocation
Advanced memory allocationAdvanced memory allocation
Advanced memory allocation
 
Paper_An Efficient Garbage Collection in Java Virtual Machine via Swap I/O O...
Paper_An Efficient Garbage Collection in Java Virtual  Machine via Swap I/O O...Paper_An Efficient Garbage Collection in Java Virtual  Machine via Swap I/O O...
Paper_An Efficient Garbage Collection in Java Virtual Machine via Swap I/O O...
 
Eko10 - Security Monitoring for Big Infrastructures without a Million Dollar ...
Eko10 - Security Monitoring for Big Infrastructures without a Million Dollar ...Eko10 - Security Monitoring for Big Infrastructures without a Million Dollar ...
Eko10 - Security Monitoring for Big Infrastructures without a Million Dollar ...
 
Serverless for High Performance Computing
Serverless for High Performance ComputingServerless for High Performance Computing
Serverless for High Performance Computing
 
Progress_190213
Progress_190213Progress_190213
Progress_190213
 
Progress_190130
Progress_190130Progress_190130
Progress_190130
 
Go at Skroutz
Go at SkroutzGo at Skroutz
Go at Skroutz
 
AWS Techniques and lessons writing low cost autoscaling GitLab runners
AWS Techniques and lessons writing low cost autoscaling GitLab runnersAWS Techniques and lessons writing low cost autoscaling GitLab runners
AWS Techniques and lessons writing low cost autoscaling GitLab runners
 
OSMC 2018 | Learnings, patterns and Uber’s metrics platform M3, open sourced ...
OSMC 2018 | Learnings, patterns and Uber’s metrics platform M3, open sourced ...OSMC 2018 | Learnings, patterns and Uber’s metrics platform M3, open sourced ...
OSMC 2018 | Learnings, patterns and Uber’s metrics platform M3, open sourced ...
 
Mender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and GolangMender.io | Develop embedded applications faster | Comparing C and Golang
Mender.io | Develop embedded applications faster | Comparing C and Golang
 
Functional Programming Principles & Patterns
Functional Programming Principles & PatternsFunctional Programming Principles & Patterns
Functional Programming Principles & Patterns
 
Move from C to Go
Move from C to GoMove from C to Go
Move from C to Go
 
Aws uk ug #8 not everything that happens in vegas stay in vegas
Aws uk ug #8   not everything that happens in vegas stay in vegasAws uk ug #8   not everything that happens in vegas stay in vegas
Aws uk ug #8 not everything that happens in vegas stay in vegas
 
JVM Performance Tuning
JVM Performance TuningJVM Performance Tuning
JVM Performance Tuning
 
Intro to Mage for Data Engineering WorkflowOrchestration
Intro to Mage for Data Engineering WorkflowOrchestrationIntro to Mage for Data Engineering WorkflowOrchestration
Intro to Mage for Data Engineering WorkflowOrchestration
 
Scio - A Scala API for Google Cloud Dataflow & Apache Beam
Scio - A Scala API for Google Cloud Dataflow & Apache BeamScio - A Scala API for Google Cloud Dataflow & Apache Beam
Scio - A Scala API for Google Cloud Dataflow & Apache Beam
 
Processing Terabytes of data every day … and sleeping at night (infiniteConf ...
Processing Terabytes of data every day … and sleeping at night (infiniteConf ...Processing Terabytes of data every day … and sleeping at night (infiniteConf ...
Processing Terabytes of data every day … and sleeping at night (infiniteConf ...
 
Hardware Accelerated Machine Learning Solution for Detecting Fraud and Money ...
Hardware Accelerated Machine Learning Solution for Detecting Fraud and Money ...Hardware Accelerated Machine Learning Solution for Detecting Fraud and Money ...
Hardware Accelerated Machine Learning Solution for Detecting Fraud and Money ...
 
Effective memory management
Effective memory managementEffective memory management
Effective memory management
 
Effective memory management
Effective memory managementEffective memory management
Effective memory management
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

Memory in go

  • 1. Memory in Go Jakarta, 16 November 2017 Meetup: GO-JEK and Go iman.t@go-jek.com
  • 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
  • 21. QUESTION? We are hiring! Contact recruitment@go-jek.com Shortcut contact: m.kautsar@gojek.com jessica.t@go-jek.com