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
+8k PHP : How to parse ElasticSearch JSON ?
+24.8k Golang : Get current file path of a file or executable
+10.1k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+13.6k Golang : Simple word wrap or line breaking example
+18k Golang : Iterating Elements Over A List
+8.8k Golang : Write multiple lines or divide string into multiple lines
+11.6k Golang : Get remaining text such as id or filename after last segment in URL path
+6.3k Golang : Skip or discard items of non-interest when iterating example
+14.2k Golang : Get URI segments by number and assign as variable example
+5.6k Golang : Function as an argument type example
+13k Golang : Get constant name from value
+6.9k Golang : File system scanning