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
+11k Golang : List running EC2 instances and descriptions
+12.2k Golang : Read from buffered reader until specific number of bytes
+5.9k Fix sudo yum hang problem with no output or error messages
+9.8k Golang : Generate random elements without repetition or duplicate
+19.7k Golang : Create and resolve(read) symbolic links
+8.8k Golang : Sort and reverse sort a slice of integers
+29.7k error: trying to remove "yum", which is protected
+15.3k Golang : Send email and SMTP configuration example
+21.3k Golang : Convert Unix timestamp to UTC timestamp
+9.4k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+22.9k Golang : Daemonizing a simple web server process example
+4.4k Golang : Issue HTTP commands to server and port example