Free GoLang Memory Management Questions and Answers — Questions and Answers
Question 1: What does garbage collection do in Go?
- Manually frees allocated memory.
- Automatically reclaims memory that is no longer in use. (Correct answer)
- Prevents memory leaks by stopping cyclic references. (Correct answer)
- Requires explicit invocation by the programmer.
Correct answer: Automatically reclaims memory that is no longer in use.
Go features an automatic garbage collector that manages memory allocation and deallocation. Its primary role is to automatically identify and reclaim memory that is no longer referenced by the program, preventing memory leaks and reducing the burden on developers for manual memory management. This process ensures efficient use of system resources and effectively handles cyclic references, which can otherwise lead to memory leaks.
Question 2: Which of the following statements about pointers in Go are true?
- Go allows pointer arithmetic.
- A pointer holds the memory address of a value. (Correct answer)
- nil is the zero value for a pointer. (Correct answer)
- The & operator is used to dereference a pointer.
Correct answer: A pointer holds the memory address of a value.
Pointers in Go store the memory address of a variable, allowing indirect access to its value. Unlike some other languages, Go explicitly disallows pointer arithmetic to maintain memory safety and simplify the language. The `&` operator takes the address of a variable, creating a pointer, while the `*` operator dereferences a pointer to access the value it points to. `nil` is indeed the zero value for a pointer, indicating it points to no valid memory address.
Question 3: Which of the following functions are used for memory allocation in Go?
- new (Correct answer)
- make (Correct answer)
- allocate
- malloc
Correct answer: new
In Go, `new` is used to allocate memory for a value of a specified type and returns a pointer to the zero-initialized value. `make` is used specifically for slices, maps, and channels, allocating and initializing their internal data structures. `allocate` and `malloc` are not Go built-in functions for memory allocation.
Question 4: What is the zero value of memory in Go when allocated with new?
- 0 for numeric types. (Correct answer)
- "" for strings. (Correct answer)
- nil for pointers. (Correct answer)
- Random garbage values for structs.
Correct answer: 0 for numeric types.
When memory is allocated with `new` in Go, the allocated value is always zero-initialized. This means numeric types like integers and floats are initialized to `0`, booleans to `false`, strings to `""` (empty string), and pointers to `nil`. Go avoids 'random garbage values' to ensure predictable behavior and prevent common programming errors.
Question 5: Which of the following methods can be used to tune or monitor Go's garbage collection?
- Setting the GOGC environment variable. (Correct answer)
- Using the runtime.GC() function. (Correct answer)
- Writing a custom garbage collector.
- Profiling garbage collection with runtime/pprof. (Correct answer)
Correct answer: Setting the GOGC environment variable.
The `GOGC` environment variable allows you to control the garbage collector's target percentage, influencing how aggressively it runs. `runtime.GC()` forces an immediate garbage collection cycle, which can be useful for specific scenarios but isn't a primary tuning mechanism. Profiling with `runtime/pprof` helps identify GC bottlenecks, but doesn't directly tune it. Go does not support writing custom garbage collectors.
What does garbage collection do in Go?