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
+12.6k Golang : HTTP response JSON encoded data
+12.9k Swift : Convert (cast) Int or int32 value to CGFloat
+19.9k Golang : Append content to a file
+22.5k Golang : Read directory content with filepath.Walk()
+14k Golang : convert(cast) string to float value
+8.7k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+15.7k Golang : rune literal not terminated error
+15.8k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+5.4k Python : Convert(cast) string to bytes example
+13.4k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+22k Golang : Convert string slice to struct and access with reflect example
+4.7k Mac OSX : Get disk partitions' size, type and name