Memory Management and Concurrency in Go
Tags: go, concurrency, goroutines, garbagecollection
Published: 2024-05-27 14:55:02

Go, developed by Google, is known for its efficiency and simplicity in handling memory management and concurrency. In this blog post, we'll explore how Go manages memory, how its garbage collector (GC) works, and the fundamentals of goroutines that enable Go's powerful concurrency model.

![](https://victorleungtw.com/static/5c3fe73d382531788a5b705fc67f77db/8aab1/2024-05-27.webp)

### Memory Management in Go

Effective memory management is crucial for any programming language, and Go handles it with a combination of efficient allocation, dynamic stack management, and garbage collection.

#### Memory Allocation

Go uses a heap for dynamic memory allocation. Here's a closer look at how memory is allocated:

- **Small Objects (≤32KB)**: These are allocated using a technique called **size classes**. Go maintains separate pools for objects of different sizes, which helps in reducing fragmentation and speeding up allocation.
- **Large Objects**: For objects larger than 32KB, Go maintains a free list of large objects. Allocation and deallocation of these objects are handled separately to optimize performance.

In Go, you can allocate memory using the `new` and `make` functions:
- `new`: Allocates zeroed storage and returns a pointer to it. It’s used for value types like integers and structures.
- `make`: Used for slices, maps, and channels. It initializes the internal data structure and returns a ready-to-use instance.

#### Stack Management

Each goroutine in Go has its own stack, starting small (e.g., 2KB) and growing as needed. This dynamic sizing allows Go to handle many goroutines efficiently without consuming too much memory upfront.

When a stack needs to grow, Go creates a new, larger stack and copies the contents of the old stack to the new one. This process is seamless and ensures that goroutines can continue to run efficiently without manual intervention.

### Garbage Collection in Go

Garbage collection is a critical component of Go's memory management system. Go uses a concurrent garbage collector, which minimizes pause times by running alongside your program. Here's a breakdown of how it works:

#### Mark-and-Sweep Algorithm

Go's GC uses a **mark-and-sweep** algorithm, consisting of two main phases:

1. **Mark**: The GC starts by marking all objects that are reachable from the root set (global variables, stack variables, etc.). This process identifies all live objects.
2. **Sweep**: After marking, the GC sweeps through the heap to reclaim memory occupied by unmarked objects, effectively cleaning up unused memory.

#### Tri-Color Marking and Write Barriers

To manage the marking process efficiently, Go employs **tri-color marking**. Objects are classified into three colors:
- **White**: Unreachable objects that can be collected.
- **Grey**: Objects that have been found but whose references have not been processed.
- **Black**: Objects that have been fully processed and are reachable.

**Write barriers** are used to handle new references created during the GC process. They ensure that any changes to the object graph are correctly tracked, maintaining the integrity of the GC process.

#### Triggering the Garbage Collector

The GC in Go is typically triggered automatically based on memory usage and allocation patterns. However, it can also be manually invoked using `runtime.GC()`. The automatic triggering occurs when:
- A certain amount of new memory has been allocated since the last collection.
- The heap size exceeds a specified threshold.
- The runtime's heuristics determine it’s necessary to balance performance and memory usage.

### Goroutines: Lightweight Concurrency

One of Go's standout features is its lightweight concurrency model, built on goroutines.

#### Creating Goroutines

Goroutines are created using the `go` keyword followed by a function call. For example:
```go
go myFunction()
```
Goroutines are much cheaper to create and manage compared to traditional OS threads, enabling the creation of thousands of concurrent tasks without significant overhead.

#### Execution and Scheduling

Goroutines are scheduled by Go's runtime scheduler, which uses **M:N scheduling**. This means multiple goroutines (N) are multiplexed onto a smaller or equal number of OS threads (M). The scheduler efficiently manages goroutine execution, ensuring that system resources are used effectively.

#### Communication via Channels

Goroutines communicate and synchronize using **channels**. Channels provide a way to send and receive values between goroutines, enabling safe and efficient data sharing without explicit locks or shared memory.

#### Dynamic Stack Growth

As mentioned earlier, goroutines start with a small stack and grow as needed. This dynamic growth helps manage memory more efficiently compared to fixed-size stacks, allowing Go to handle large numbers of concurrent goroutines.

### Conclusion

Go's memory management and concurrency model are key factors in its performance and simplicity. The combination of efficient memory allocation, a sophisticated garbage collector, and lightweight goroutines makes Go a powerful choice for building scalable and high-performance applications. Understanding these core concepts will help you leverage Go's full potential in your projects.