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
+11.4k Golang : Exit, terminating or aborting a program
+9.1k Golang : How to tokenize source code with text/scanner package?
+8.2k Random number generation with crypto/rand in Go
+24.2k Golang : How to read integer value from standard input ?
+18.3k Golang : Determine if directory is empty with os.File.Readdir() function
+8.9k Golang : Channels and buffered channels examples
+14.3k Golang : invalid character ',' looking for beginning of value
+10.4k Get form post value in Go
+6.5k Golang : Detect sample rate, channels or latency with PortAudio
+5.8k How to let Facebook Login button redirect to a particular URL ?
+12.1k Swift : Convert (cast) Int to String ?