Golang : Reclaim memory occupied by make() example
Problem :
You have allocated some memory for buffer via the make()
function and you want to reclaim back the occupied memory after done using the buffer. How to do that?
Solution :
For example :
buffer := make([]byte, 1024)
or
buffer := make([][]int, row)
to reclaim back the memory, simply do :
buffer = nil
Golang's garbage collector will automatically free up the memory or you can trigger garbage collection manually as well with runtime/debug.FreeOSMemory()
References :
https://www.socketloop.com/tutorials/golang-how-to-get-garbage-collection-data
https://www.socketloop.com/tutorials/golang-when-to-use-make-or-new
See also : Golang : When to use make or new?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.9k Golang : Levenshtein distance example
+12k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+5k Golang : Extract unicode string from another unicode string example
+12.8k Android Studio : Use image as AlertDialog title with custom layout example
+5.7k Golang : Check if password length meet the requirement
+14k Golang : Force download file example
+15.6k Golang : Get input from keyboard
+7.9k Golang : Apply Histogram Equalization to color images
+10.4k Golang : flag provided but not defined error
+8.2k Golang : Create unique title slugs example
+21.6k Golang : Calculate time different
+17.7k Golang : Calculate entire request body length during run time