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
+17.9k Golang : Get command line arguments
+18.5k Golang : Padding data for encryption and un-padding data for decryption
+17.3k Golang : Defer function inside init()
+4.9k PHP : See installed compiled-in-modules
+10k Golang : Embed secret text string into binary(executable) file
+7k Golang : Of hash table and hash map
+13.9k Golang : syscall.Socket example
+15.9k Golang : How to extract links from web page ?
+5.3k Golang : Frobnicate or tweaking a string example
+19.5k Golang : Count JSON objects and convert to slice/array
+7.7k Golang : Check from web if Go application is running or not
+5.7k PHP : Get client IP address