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
+5.2k Golang : Join lines with certain suffix symbol example
+4.8k Unix/Linux : Get reboot history or check when was the last reboot date
+5.1k Golang : Missing Subversion command
+28k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+6.3k Golang : How to stop user from directly running an executable file?
+9.4k Golang : Simple File Server
+28.1k Golang : Get time.Duration in year, month, week or day
+14.9k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+9.2k Golang : Allow Cross-Origin Resource Sharing request
+5k Golang : Shuffle array of list
+16.5k Golang : Simple client server example