SlideShare a Scribd company logo
Advanced memory
allocation in Go
Joris Bonnefoy
DevOps @ OVH
@devatoria
Before we get started...
Keep it simple, readable
● Avoid premature optimizations
○ Sometimes, readability and logicalness are better than performances
● Use go tools in order to point out real problems
○ pprof
○ gcflags
Introduction to
memory management
Virtual and physical memory
● When you launch a new process, the kernel
creates its address space
● In this address space, the process can
allocate memory
● For the process, its address space looks like
a big contiguous memory space
● For two identical processes, (logical)
addresses can be the same
○ Depending on the compiler, the architecture, ...
Virtual and physical memory
● Each process has its own “memory
sandbox” called its virtual address
space
● Virtual addresses are mapped to
physical addresses by the CPU (and
the MMU component) using page
tables
● Per-process virtual space size is
4GB on 32-bits system and 256TB
on 64-bits one
Virtual and physical memory
● Virtual address space is split into 2 spaces
○ Kernel space
○ User mode space (or process address space)
● Split depends on operating system
configuration
How is user space managed? (C example)
Stack vs. Heap
The stack
● On the top of the process address space
○ Grows down
● Last In First Out design
○ Cheap allocation cost
● Only one register is needed to track content
○ Stack Pointer (SP register)
● Calling a function pushes a new stack frame
onto the stack
● Stack frame is destroyed on function return
● Each thread in a process has its own stack
○ They share the same address space
● Stack size is limited, but can be expanded
○ Until a certain limit, usually 8MB
The heap
● Grows up
● Expensive allocation cost
○ No specific data structure
● Complex management
● Used to allocate variables that must
outlive the function doing the allocation
● Fragmentation issues
○ Contiguous free blocks can be merged
Memory allocation
in Go
Like threads, goroutines have
their own stack.
<= Go 1.2 - Segmented stacks
● Discontiguous stacks
● Grows incrementally
● Each stack starts with a segment (8kB in Go 1.2)
● When stack is full
a. another segment is created and linked to the stack
b. stack segment is removed when not used anymore (stack is shrinked)
● Stacks are doubly-linked list
<= Go 1.2 - Segmented stacks
<= Go 1.2 - Hot split issue
>= Go 1.3 - Copying stacks
● Contiguous stacks
● Each stack starts with a size of 2kB (since Go 1.4)
● When stack is full
a. a new stack is created, with the double size of the previous one
b. content of the old one is copied to the new one
c. pointers are re-adjusted
d. old one is destroyed
● Stack is never shrinked
>= Go 1.3 - Copying stacks
Benchmarks
Efficient memory
allocation (and compiler
optimizations)
Reminders
Heap
(de)allocation
is expensive
Stack
(de)allocation
is cheap
Reminders
Go manages memory automatically
Reminders
Go prefers allocation on the stack
Go functions
inlining
Go functions inlining
● The code of the inlined function is inserted at the place of each call to this function
● No more assembly CALL instruction
● No need to create function stack frame
● Binary size is increased because of the possible repetition of assembly instructions
● Can be disabled using //go:noinline comment just before the function declaration
Go escape
analysis system
Escape analysis
● Decides whether a variable should be allocated on the heap or on the stack
● Creates a graph of function calls in order to track variables scope
● Uses tracking data to pass checks on those variables
○ Those checks are not explicitly detailed in Go specs
● If checks pass, the variable is allocated on the stack (it doesn’t escape)
● If at least one check fails, the variable is allocated on the heap (it escapes)
● Escape analysis results can be checked at compile time using
○ go build -gcflags '-m' ./main.go
One basic rule (not always right…)
If a variable has its address taken,
that variable is a candidate for allocation on the heap
Closure calls
Closure calls are not analyzed
Assignments to slices and maps
A map can be allocated on the stack,
but any keys or values inserted into the map will escape
Flow through channels
A variable passing through a channel
will always escape
Interfaces
Interfaces can lead to escape
when a function of the given interface is called
(because the compiler doesn’t know
what the function is doing with its arguments)
And a lot of other cases...
● Go escape analysis is very simple and not so smart
● Some issues are opened to improve it
Conclusion
Keep it simple, readable
● Avoid premature optimizations
○ Sometimes, readability and logicalness are better than performances
● Use go tools in order to point out real problems
○ pprof
○ gcflags
Remember the basics
● Pointers are only useful if you directly manipulate variable value
○ Most of the time, a copy of the value is sufficient
● Closures are not always sexy
○ Do not overuse them just to overuse them
● Manipulate arrays when possible
○ Slices are cool, but arrays too... :)
Thank you
for listening!
References
● https://segment.com/blog/allocation-efficiency-in-high-performance-go-services/
● https://en.wikipedia.org/wiki/Stack-based_memory_allocation
● http://gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html
● https://dave.cheney.net/2014/06/07/five-things-that-make-go-fast
● https://blog.cloudflare.com/how-stacks-are-handled-in-go/
● http://www.tldp.org/LDP/tlk/mm/memory.html
● http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/
● http://agis.io/2014/03/25/contiguous-stacks-in-go.html
● https://medium.com/@felipedutratine/does-golang-inline-functions-b41ee2d743fa
● https://docs.google.com/document/d/1CxgUBPlx9iJzkz9JWkb6tIpTe5q32QDmz8l0BouG0Cw/preview

More Related Content

PPTX
Diagnosing HotSpot JVM Memory Leaks with JFR and JMC
PPTX
Distributed Transaction Management in Spring & JEE
PDF
LSTM Basics
PDF
BKK16-300 Benchmarking 102
PDF
MongoDB Operational Best Practices (mongosf2012)
PPTX
Data structure introduction
ODP
work load characterization
PPTX
Concurrency
Diagnosing HotSpot JVM Memory Leaks with JFR and JMC
Distributed Transaction Management in Spring & JEE
LSTM Basics
BKK16-300 Benchmarking 102
MongoDB Operational Best Practices (mongosf2012)
Data structure introduction
work load characterization
Concurrency

Viewers also liked (20)

PPTX
In-Memory Computing Essentials for Architects and Engineers
PPTX
Communication hardware
PDF
numPYNQ @ NGCLE@e-Novia 15.11.2017
PPTX
Docker Networking
PDF
[若渴計畫] Challenges and Solutions of Window Remote Shellcode
PDF
Scale Up with Lock-Free Algorithms @ JavaOne
PPTX
Walk through an enterprise Linux migration
PPTX
Graduating To Go - A Jumpstart into the Go Programming Language
PPTX
What in the World is Going on at The Linux Foundation?
PPT
DevRomagna / Golang Intro
PDF
OCCIware, an extensible, standard-based XaaS consumer platform to manage ever...
PDF
Linux Security APIs and the Chromium Sandbox (SwedenCpp Meetup 2017)
PDF
Go Execution Tracer
PPTX
Server virtualization
PPTX
Virtualization
PPTX
OpenFlow
PPTX
SDN Architecture & Ecosystem
PDF
In-depth forensic analysis of Windows registry files
PDF
Deep dive into Coroutines on JVM @ KotlinConf 2017
PPTX
Network Virtualization
In-Memory Computing Essentials for Architects and Engineers
Communication hardware
numPYNQ @ NGCLE@e-Novia 15.11.2017
Docker Networking
[若渴計畫] Challenges and Solutions of Window Remote Shellcode
Scale Up with Lock-Free Algorithms @ JavaOne
Walk through an enterprise Linux migration
Graduating To Go - A Jumpstart into the Go Programming Language
What in the World is Going on at The Linux Foundation?
DevRomagna / Golang Intro
OCCIware, an extensible, standard-based XaaS consumer platform to manage ever...
Linux Security APIs and the Chromium Sandbox (SwedenCpp Meetup 2017)
Go Execution Tracer
Server virtualization
Virtualization
OpenFlow
SDN Architecture & Ecosystem
In-depth forensic analysis of Windows registry files
Deep dive into Coroutines on JVM @ KotlinConf 2017
Network Virtualization
Ad

Similar to Advanced memory allocation (20)

PDF
Move from C to Go
PDF
Memory Management in Go: Stack, Heap & Garbage Collector
PPTX
golang_getting_started.pptx
PPTX
Memory in go
PDF
Golang and Eco-System Introduction / Overview
PDF
Introduction to Programming in Go
PPTX
The Go Programing Language 1
PPTX
Should i Go there
PDF
Goroutine stack and local variable allocation in Go
PPTX
PPTX
Golang workshop - Mindbowser
PDF
Go introduction
PDF
golang_refcard.pdf
PPTX
The GO Language : From Beginners to Gophers
PDF
Go language presentation
PPTX
Go Programming Language (Golang)
PDF
Garbage collector и управление памятью в Go
PDF
marko_go_in_badoo
PDF
Golang
Move from C to Go
Memory Management in Go: Stack, Heap & Garbage Collector
golang_getting_started.pptx
Memory in go
Golang and Eco-System Introduction / Overview
Introduction to Programming in Go
The Go Programing Language 1
Should i Go there
Goroutine stack and local variable allocation in Go
Golang workshop - Mindbowser
Go introduction
golang_refcard.pdf
The GO Language : From Beginners to Gophers
Go language presentation
Go Programming Language (Golang)
Garbage collector и управление памятью в Go
marko_go_in_badoo
Golang
Ad

Recently uploaded (20)

PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
history of c programming in notes for students .pptx
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
Introduction to Artificial Intelligence
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
ISO 45001 Occupational Health and Safety Management System
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Digital Strategies for Manufacturing Companies
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
L1 - Introduction to python Backend.pptx
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Transform Your Business with a Software ERP System
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Which alternative to Crystal Reports is best for small or large businesses.pdf
history of c programming in notes for students .pptx
ManageIQ - Sprint 268 Review - Slide Deck
Introduction to Artificial Intelligence
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
How to Choose the Right IT Partner for Your Business in Malaysia
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
ISO 45001 Occupational Health and Safety Management System
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Digital Strategies for Manufacturing Companies
Online Work Permit System for Fast Permit Processing
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Design an Analysis of Algorithms I-SECS-1021-03
L1 - Introduction to python Backend.pptx
Upgrade and Innovation Strategies for SAP ERP Customers
Transform Your Business with a Software ERP System
Lecture 3: Operating Systems Introduction to Computer Hardware Systems

Advanced memory allocation

  • 2. Joris Bonnefoy DevOps @ OVH @devatoria
  • 3. Before we get started...
  • 4. Keep it simple, readable ● Avoid premature optimizations ○ Sometimes, readability and logicalness are better than performances ● Use go tools in order to point out real problems ○ pprof ○ gcflags
  • 6. Virtual and physical memory ● When you launch a new process, the kernel creates its address space ● In this address space, the process can allocate memory ● For the process, its address space looks like a big contiguous memory space ● For two identical processes, (logical) addresses can be the same ○ Depending on the compiler, the architecture, ...
  • 7. Virtual and physical memory ● Each process has its own “memory sandbox” called its virtual address space ● Virtual addresses are mapped to physical addresses by the CPU (and the MMU component) using page tables ● Per-process virtual space size is 4GB on 32-bits system and 256TB on 64-bits one
  • 8. Virtual and physical memory ● Virtual address space is split into 2 spaces ○ Kernel space ○ User mode space (or process address space) ● Split depends on operating system configuration
  • 9. How is user space managed? (C example)
  • 11. The stack ● On the top of the process address space ○ Grows down ● Last In First Out design ○ Cheap allocation cost ● Only one register is needed to track content ○ Stack Pointer (SP register) ● Calling a function pushes a new stack frame onto the stack ● Stack frame is destroyed on function return ● Each thread in a process has its own stack ○ They share the same address space ● Stack size is limited, but can be expanded ○ Until a certain limit, usually 8MB
  • 12. The heap ● Grows up ● Expensive allocation cost ○ No specific data structure ● Complex management ● Used to allocate variables that must outlive the function doing the allocation ● Fragmentation issues ○ Contiguous free blocks can be merged
  • 14. Like threads, goroutines have their own stack.
  • 15. <= Go 1.2 - Segmented stacks ● Discontiguous stacks ● Grows incrementally ● Each stack starts with a segment (8kB in Go 1.2) ● When stack is full a. another segment is created and linked to the stack b. stack segment is removed when not used anymore (stack is shrinked) ● Stacks are doubly-linked list
  • 16. <= Go 1.2 - Segmented stacks
  • 17. <= Go 1.2 - Hot split issue
  • 18. >= Go 1.3 - Copying stacks ● Contiguous stacks ● Each stack starts with a size of 2kB (since Go 1.4) ● When stack is full a. a new stack is created, with the double size of the previous one b. content of the old one is copied to the new one c. pointers are re-adjusted d. old one is destroyed ● Stack is never shrinked
  • 19. >= Go 1.3 - Copying stacks
  • 21. Efficient memory allocation (and compiler optimizations)
  • 26. Go functions inlining ● The code of the inlined function is inserted at the place of each call to this function ● No more assembly CALL instruction ● No need to create function stack frame ● Binary size is increased because of the possible repetition of assembly instructions ● Can be disabled using //go:noinline comment just before the function declaration
  • 28. Escape analysis ● Decides whether a variable should be allocated on the heap or on the stack ● Creates a graph of function calls in order to track variables scope ● Uses tracking data to pass checks on those variables ○ Those checks are not explicitly detailed in Go specs ● If checks pass, the variable is allocated on the stack (it doesn’t escape) ● If at least one check fails, the variable is allocated on the heap (it escapes) ● Escape analysis results can be checked at compile time using ○ go build -gcflags '-m' ./main.go
  • 29. One basic rule (not always right…) If a variable has its address taken, that variable is a candidate for allocation on the heap
  • 30. Closure calls Closure calls are not analyzed
  • 31. Assignments to slices and maps A map can be allocated on the stack, but any keys or values inserted into the map will escape
  • 32. Flow through channels A variable passing through a channel will always escape
  • 33. Interfaces Interfaces can lead to escape when a function of the given interface is called (because the compiler doesn’t know what the function is doing with its arguments)
  • 34. And a lot of other cases... ● Go escape analysis is very simple and not so smart ● Some issues are opened to improve it
  • 36. Keep it simple, readable ● Avoid premature optimizations ○ Sometimes, readability and logicalness are better than performances ● Use go tools in order to point out real problems ○ pprof ○ gcflags
  • 37. Remember the basics ● Pointers are only useful if you directly manipulate variable value ○ Most of the time, a copy of the value is sufficient ● Closures are not always sexy ○ Do not overuse them just to overuse them ● Manipulate arrays when possible ○ Slices are cool, but arrays too... :)
  • 39. References ● https://segment.com/blog/allocation-efficiency-in-high-performance-go-services/ ● https://en.wikipedia.org/wiki/Stack-based_memory_allocation ● http://gribblelab.org/CBootCamp/7_Memory_Stack_vs_Heap.html ● https://dave.cheney.net/2014/06/07/five-things-that-make-go-fast ● https://blog.cloudflare.com/how-stacks-are-handled-in-go/ ● http://www.tldp.org/LDP/tlk/mm/memory.html ● http://duartes.org/gustavo/blog/post/anatomy-of-a-program-in-memory/ ● http://agis.io/2014/03/25/contiguous-stacks-in-go.html ● https://medium.com/@felipedutratine/does-golang-inline-functions-b41ee2d743fa ● https://docs.google.com/document/d/1CxgUBPlx9iJzkz9JWkb6tIpTe5q32QDmz8l0BouG0Cw/preview