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
+32.1k Golang : Convert []string to []byte examples
+4.9k Golang : micron to centimeter example
+15.1k JavaScript/JQuery : Detect or intercept enter key pressed example
+9.9k Golang : Translate language with language package example
+4.6k Golang : How to pass data between controllers with JSON Web Token
+7.2k Golang : Of hash table and hash map
+18.1k Golang : Get command line arguments
+14k Golang : Reverse IP address for reverse DNS lookup example
+6.5k Unix/Linux : How to get own IP address ?
+7.6k Golang : Convert(cast) io.Reader type to string
+16.3k Golang : Find out mime type from bytes in buffer
+6.9k Mac OSX : Find large files by size