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
+6.5k Golang : Output or print out JSON stream/encoded data
+5.1k PHP : Hide PHP version information from curl
+18.3k Golang : Find IP address from string
+9.3k Golang : How to extract video or image files from html source code
+18.5k Unmarshal/Load CSV record into struct in Go
+4.5k JavaScript: Add marker function on Google Map
+9.5k Golang : Load ASN1 encoded DSA public key PEM file example
+13.1k Golang : Verify token from Google Authenticator App
+4.9k Golang : Experimental Jawi programming language
+12.1k Golang : Validate email address
+33.6k Golang : convert(cast) bytes to string
+4.6k Which content-type(MIME type) to use for JSON data