Learning GoLang - Review : Memory Management
An Example
If you are used to programming in C or C++ ways, you must get mad when you see someone wrote a function like this:
*BlockChain CreateBlockChain(){
BlockChain blockchain;
blockchain.Blocks.push_back(GenesisBlock())
return &blockchain
}
The huge issue here is the memory management of the local variable blockchain
is completely incorrect. You can never return an address of a local variable, which means the memory of it would be released out of its scope.
However, this is possible in Go.
func CreateBlockChain() *BlockChain{
blockchain := BlockChain{}
blockchain.Blocks = append(blockchain.Blocks, GenesisBlock())
return &blockchain
}
There are basically two reasons:
- Automatic Memory Management: In Go, memory is managed automatically by a garbage collector. This means that variables allocated on the stack or heap are automatically reclaimed when they are no longer in use. When a local variable is returned from a function, Go ensures that the memory for that variable remains valid until it's no longer needed by the program. This means that even though the variable goes out of scope when the function returns, Go's garbage collector ensures that the memory occupied by the variable is reclaimed only when it's no longer in use by the program.
- Escape Analysis: Go's compiler performs escape analysis to determine if a variable's lifetime extends beyond its current scope. If a variable's address is taken and it escapes the local scope (e.g., returned from a function), Go will allocate memory for it on the heap instead of the stack. This ensures that the variable remains valid even after the function returns.
Another example helps you to better understand:
package main
import "fmt"
func createLocalVariable() *int {
localVar := 42
return &localVar
}
func main() {
ptr := createLocalVariable()
fmt.Println("Value at ptr:", *ptr)
}
Even though localVar
goes out of scope when createLocalVariable
returns, the memory allocated for it is reclaimed by the garbage collector only when it's no longer in use by the program. In this case, it remains valid because ptr
still holds a reference to it, and we can safely access the value pointed to by ptr
in the main function.
Safer Pointer
Additionally, Go pointers are safer compared to C pointers because they don't allow direct memory manipulation like in C. Go pointers are bound by the language's safety rules, preventing common pointer-related errors such as accessing uninitialized memory or dangling pointers.
In conclusion, Go's memory management model, including garbage collection and escape analysis, ensures that returning a pointer to a local variable from a function is safe and doesn't lead to memory corruption or undefined behavior, as it might in C.