Memory/Ownership
30 articles
Can You Do Pointer Arithmetic in Go
Go forbids direct pointer arithmetic for safety, requiring the unsafe package for manual memory offset calculations.
Common Causes of Memory Leaks: Goroutines, Slices, Maps, Timers
Identify and fix Go memory leaks caused by blocked goroutines, unbounded slices/maps, and unstopped timers using pprof and proper resource cleanup.
How Double Pointers Work in Go (**T)
Go uses **T syntax for double pointers to allow functions to modify the address stored in a pointer variable.
How Escape Analysis Works in Go
Go's escape analysis determines at compile time if variables stay on the stack or move to the heap to optimize memory usage.
How Go Manages Memory: Tiny, Small, and Large Allocations
Go optimizes memory by routing tiny, small, and large allocations to specialized paths for efficiency.
How Memory Management Works in Go
Go uses automatic garbage collection with optional manual arena allocation for bulk memory management, configurable via GODEBUG settings.
How Pointers Work with Slices and Maps in Go
Slices and maps in Go are reference types that share underlying data, allowing functions to modify the original values directly without explicit pointers.
How Stack vs Heap Allocation Works in Go
Go automatically places variables on the stack or heap based on their lifetime, which you can inspect using the -gcflags=-m compiler flag.
How to Avoid Nil Pointer Dereference in Go
Prevent nil pointer dereferences in Go by checking if pointers are nil before accessing their fields or methods.
How to Create a Pointer to a Literal Value in Go
Assign the literal to a variable first, then use the address-of operator (&) to create a pointer to that variable.
How to Declare and Use Pointers in Go
Declare Go pointers with `*`, get addresses with `&`, and dereference values with `*` to modify variables directly.
How to Dereference a Pointer in Go
To dereference a pointer in Go, use the asterisk (`*`) operator on the pointer variable to access the underlying value it points to.
How to Detect Memory Leaks in Go
Detect Go memory leaks using the runtime memory profiler with pprof or by compiling with AddressSanitizer (asan) to identify unreleased memory allocations.
How to Pass a Pointer to a Function in Go
To pass a pointer to a function in Go, declare the parameter type with an asterisk (e.g., `*int`) and pass the address of the variable using the `&` operator.
How to Return a Pointer from a Function in Go
You return a pointer by declaring the function's return type with an asterisk (e.g., `*MyType`) and returning the address of a variable using the `&` operator.
How to Use Arenas (arena Package) for Manual Memory Management in Go
Use the Go arena package with the goexperiment.arenas tag to manually allocate and free bulk memory blocks for better performance.
How to Use runtime.SetFinalizer in Go
Attach a cleanup function to a Go object to run automatically when the garbage collector reclaims it.
How to Use Struct Pointers in Go
Use struct pointers in Go by declaring variables with the `*` prefix and accessing fields directly via the dot operator.
How to Use the new() Function in Go
The `new()` function in Go allocates zeroed memory for a type and returns a pointer to it, whereas `make()` is used only for slices, maps, and channels to initialize their internal data structures.
Memory Layout of Go Types: Structs, Slices, Maps, Interfaces
Go structs are contiguous memory blocks, while slices, maps, and interfaces are heap-allocated references managed by headers or pointers.
Pointer vs Value: When to Use Pointers in Go
Use pointers in Go to modify data in place or avoid copying large structs, and use values for small, immutable data.
Stack vs Heap Allocation in Go: How Escape Analysis Works
Go's escape analysis automatically moves variables from the stack to the heap if their lifetime exceeds the function scope.
Value Receiver vs Pointer Receiver in Go: When to Use Which
Use pointer receivers to modify state or avoid copying large structs, and value receivers for read-only operations on small types.
What Are Pointers in Go and Why Use Them
Go pointers store memory addresses to modify variables directly and avoid expensive data copying.
What Do * and & Mean in Go
In Go, * dereferences a pointer to access its value, while & takes the memory address of a variable.
What Is a Memory Leak in Go and How to Find One
A memory leak in Go is unreleased memory, often found using pprof or LSAN for cgo, causing gradual resource exhaustion.
What Is a Nil Pointer in Go
A nil pointer in Go is a null reference that causes a runtime error when used with panic in Go 1.21+ unless the panicnil GODEBUG setting is enabled.
What Is the Difference Between new and make in Go
Use new for zeroed pointers and make for initialized slices, maps, and channels.
When to Use a Pointer to a Struct vs a Value in Go
Use pointers for large or mutable structs to avoid copying, and values for small or immutable data to ensure safety.
When to Use Pointers vs Values in Go
Use values for small, immutable types and pointers for large structs or when modifying the original data is required.