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.5k Generate Random number with math/rand in Go
+55.4k Golang : Unmarshal JSON from http response
+5.2k Golang : Experimental Jawi programming language
+8.5k PHP : How to parse ElasticSearch JSON ?
+18.4k Golang : Read binary file into memory
+11.5k Golang : Generate DSA private, public key and PEM files example
+7k Golang : How to call function inside template with template.FuncMap
+4.1k Detect if Google Analytics and Developer Media are loaded properly or not
+8.3k Golang : Find relative luminance or color brightness
+12.4k Golang : Validate email address
+14.6k Golang : Missing Bazaar command
+9.8k Golang : List available AWS regions