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
+8.5k Golang : Progress bar with ∎ character
+4.6k Javascript : Access JSON data example
+8.7k Yum Error: no such table: packages
+3.3k Golang : Fix go-cron set time not working issue
+19.5k Golang : Close channel after ticker stopped example
+9.8k Golang : Turn string or text file into slice example
+17.4k Golang : Find smallest number in array
+5.8k Facebook : How to force facebook to scrape latest URL link data?
+6.9k Golang : Find the shortest line of text example
+10.7k Android Studio : Checkbox for user to select options example
+41.6k Golang : How do I convert int to uint8?
+5.6k Fix fatal error: evacuation not done in time problem