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.1k Golang : Generate Codabar
+12.7k Golang : convert rune to unicode hexadecimal value and back to rune character
+5.6k Golang : How to setup a disk space used monitoring service with Telegram bot
+5.5k Unix/Linux : Use netstat to find out IP addresses served by your website server
+12.8k Golang : Chunk split or divide a string into smaller chunk example
+26.4k Golang : Move file to another directory
+8k Golang : How to check if a string with spaces in between is numeric?
+5.8k Golang : Pat multiplexer routing example
+6.5k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+11.2k Golang : How to check if a string starts or ends with certain characters or words?
+5.2k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+22.1k Golang : Print out struct values in string format