Golang : Ways to recover memory during run time.
Problem :
Most program written with Golang should have automatic garbage collection to recover the used memory. However, there are times when your program process large amount of data in memory or run for a long time and you want to trigger garbage collection manually. How to do that ?
Solution :
Use runtime/debug.FreeOSMemory()
function or runtime.GC()
function.
Chances are ... your code will be compiled into a binary and deployed at a server. It will be great if your program can accept parameter such as at what time to trigger a manual garbage collection. Time such as ... when everyone is asleep or off peak hours.
NOTE : Many years ago when I was a sys admin in Vodafone New Zealand. The Java program that I was looking after will trigger a garbage collection during peak office hours... effectively locking out every users for 45 to 70 minutes! The only way to shorten the process ... is to reboot the server! As a developer, it is your responsibility to ensure manual garbage collection does not run for long time(less than 1 minute) and create bad user experience.
References :
See also : Golang : Get number of CPU cores
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 : How to determine a prime number?
+7.7k Golang : Example of how to detect which type of script a word belongs to
+15.5k Golang : How to convert(cast) IP address to string?
+21.5k Golang : GORM create record or insert new record into database example
+6.4k Grep : How to grep for strings inside binary data
+26.7k Golang : Force your program to run with root permissions
+16.7k Golang : Get the IPv4 and IPv6 addresses for a specific network interface
+30.7k Golang : Download file example
+29.1k Golang : missing Git command
+19.9k Golang : How to run your code only once with sync.Once object
+17.7k Golang : How to make a file read only and set it to writable again?
+16.3k Golang : Test floating point numbers not-a-number and infinite example