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
+15.1k Golang : Accurate and reliable decimal calculations
+19.8k Golang : How to get time from unix nano example
+6k Golang : Get missing location after unmarshal binary and gob decode time.
+8.1k Golang : Check if integer is power of four example
+4.6k Chrome : How to block socketloop.com links in Google SERP?
+19.8k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+40.8k Golang : How to check if a string contains another sub-string?
+10.6k Android Studio : Checkbox for user to select options example
+10.3k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+7.4k SSL : How to check if current certificate is sha1 or sha2 from command line
+15.2k Golang : Find location by IP address and display with Google Map
+16.4k Golang : Delete files by extension