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
+9.7k Golang : Setting variable value with ldflags
+11.7k Golang : Determine if time variables have same calendar day
+12.3k Golang : Get absolute path to binary for os.Exec function with exec.LookPath
+9k Golang : Temperatures conversion example
+24.9k Golang : Storing cookies in http.CookieJar example
+29.5k Golang : How to get HTTP request header information?
+10.2k Swift : Convert (cast) String to Integer
+20.4k Golang : Saving private and public key to files
+11.2k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+10.9k CodeIgniter : How to check if a session exist in PHP?
+8k Golang : Check if integer is power of four example
+18.1k Golang : How to get hour, minute, second from time?