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
+10.7k Golang : Select region of interest with mouse click and crop from image
+39.2k Golang : How to iterate over a []string(array)
+7.5k Golang : Convert source code to assembly language
+7.1k Golang : Find the shortest line of text example
+14.8k Golang : Send email with attachment(RFC2822) using Gmail API example
+21.9k Golang : Convert string slice to struct and access with reflect example
+8.8k Golang : Heap sort example
+5.4k Golang : Generate Interleaved 2 inch by 5 inch barcode
+25.4k Golang : Get current file path of a file or executable
+8.4k Golang : Configure Apache and NGINX to access your Go service example
+4.7k Mac OSX : Get disk partitions' size, type and name
+10.7k Golang : Simple File Server