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
+18.3k Golang : Count JSON objects and convert to slice/array
+5.5k Golang : How to determine if request or crawl is from Google robots
+11.1k Golang : Display list of countries and ISO codes
+5.8k Mac OSX : Find large files by size
+6.5k SSL : How to check if current certificate is sha1 or sha2 from command line
+11.2k Golang : Validate email address
+31.2k Golang : Smarter Error Handling with strings.Contains()
+28.9k Golang : Generate random string
+10.2k Golang : Post data with url.Values{}
+35.6k Upload multiple files with Go
+24.4k Golang : How to write CSV data to file
+27.9k Golang : missing Git command