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
+31.4k Golang : How to convert(cast) string to IP address?
+14.2k Golang : Parsing or breaking down URL
+6.8k Golang : How to setup a disk space used monitoring service with Telegram bot
+13.8k Golang : Compress and decompress file with compress/flate example
+12.4k Golang : HTTP response JSON encoded data
+9.1k Golang : How to control fmt or log print format?
+6k Linux/Unix : Commands that you need to be careful about
+22.4k Generate checksum for a file in Go
+27.2k Golang : Convert CSV data to JSON format and save to file
+62.5k Golang : Convert HTTP Response body to string
+7.5k Golang : Mapping Iban to Dunging alphabets
+8.6k Golang : Combine slices but preserve order example