Go Does Not Need a Java Style GC

Why does Go not need a fancy expensive garbage collector like Java and C#?

Why Java Needs a Fast GC More Than Everybody Else

class Knight {
int health;
}
class oopDesc {
volatile markOop _mark; // for mark and sweep
Klass* _klass; // the type
}

Memory Fragmentation

How Java overcomes memory fragmentation

Adding Complexity

How Modern Languages Avoided The Same Pitfalls as Java

// Go: Make an an array of 15 000 Point objects intype Point struct {
X, Y int
}
var points [15000]Point

Value Types

type Rect struct {
Min, Max Point
}
On the left Java style memory fragmentation. On the right contiguous blocks of memory possible in Go, C/C++, Julia etc.
type Sha1 struct {
data [20]byte
}

Value Types Are Not Enough

var ptr *Point = &rect.Min // Store pointer to Min in ptr
*ptr = Point(2, 4) // replace rect.Min

Custom Secondary Allocators

type Arena []Node

func (arena *Arena) Alloc() *Node {
if len(*arena) == 0 {
*arena = make([]Node, 10000)
}

n := &(*arena)[len(*arena)-1]
*arena = (*arena)[:len(*arena)-1]
return n
}
func buildTree(item, depth int, arena *Arena) *Node {
n := arena.Alloc()
if depth <= 0 {
*n = Node{item, nil, nil}
} else {
*n = Node{
item,
buildTree(2*item-1, depth-1, arena),
buildTree(2*item, depth-1, arena),
}
}
return n
}
n := &(*arena)[len(*arena)-1]

Problem with Java Bump Allocators

Generational GC and Escape Analysis

func escapingPtr() []int {
values := []int{4, 5, 10}
return values
}

fun nonEscapingPtr() int {
values = []int{4, 5, 10}
var total int = addUp(values)
return total
}

Modern Languages Don’t Need Compacting GCs

Generational GC vs Concurrent GC Pauses

What if Garbage is Created Faster Than Go Can Clean It Up?

In Short

Assumed GC Tradeoffs No Longer Apply

Old Techniques Are Inherently Bad?

Go is Not C#

Compaction

Heap Overhead

Why Low Latency Matters Even for Java

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store