- Allocation - managing memory allocation on the stack vs heap
- Pointers or References - sharing access to the same structure
- Finalizers - managed non-memory resources through objects
- Garbage Collection - controlling the garbage collector or automatic deallocation
- Weak References - references to shared objects that don't keep them alive
Memory Management
Change TopicGo Language
Struct Allocation: Stack vs Heap
A function can allocate a struct either on the stack or heap, depending on how it is returned or stored.
type Data struct { Value int } // Allocates on stack func stackAlloc() Data { return Data{Value: 42} // Struct allocated on stack } // Allocates on heap func heapAlloc() *Data { return &Data{Value: 42} // Struct allocated on heap } func main() { s := stackAlloc() // Stored directly in main stack frame h := heapAlloc() // Pointer to heap-allocated struct fmt.Println("Stack struct:", s) fmt.Println("Heap struct:", *h) }
Pointers vs References
Go uses pointers but does not have references like C++. A pointer holds the memory address of a variable.
x := 42 p := &x // Pointer to x (*int) fmt.Println("Value of x:", x) fmt.Println("Pointer p holds address:", p) fmt.Println("Dereferenced value:", *p) // Access value via pointer
Pointers allow modifying variables via function arguments.
func modify(val *int) { *val = 100 } x := 42 modify(&x) fmt.Println("Modified x:", x) // 100
Note that for arrays, using a slice acts like a pointer:
func modifySlice(s []int) { s[0] = 100 // This modifies the original slice } func main() { s := []int{1, 2, 3} modifySlice(s) fmt.Println("Modified slice:", s) // [100, 2, 3] }
Finalizers
Handling Resources (File Handles, Network Connections). Go relies on defer
for proper resource management:
func main() { file, err := os.Open("example.txt") if err != nil { fmt.Println("Error:", err) return } defer file.Close() // Ensures file is closed when function exits }
Garbage Collection
You can fine-tune the garbage collector using:
func main() { runtime.GC() // Manually trigger garbage collection (not recommended in normal usage) }
Weak References
Go does not support weak references natively like some other languages. However, you can simulate weak references using runtime.SetFinalizer()
to attach a destructor to an object:
type Object struct{} func main() { obj := &Object{} runtime.SetFinalizer(obj, func(o *Object) { fmt.Println("Object finalized") }) }
This runs the finalizer before garbage collection removes the object.