Is there a way in go to cause an application to write a heapdump to a file when it is out of memory?
I am looking for a global solution to the application.
Is there a way in go to cause an application to write a heapdump to a file when it is out of memory?
I am looking for a global solution to the application.
there is a profiler tool for this go tool pprof
and also pprof
package. The pprof
package will allow you to get information about current memory allocations, as well as total (cumulative) memory allocations. There is also an option to create a HTTP endpoint for profiling with net/http/pprof
.
For the simpler implementation (without HTTP), you just import runtime/pprof
and then call pprof.WriteHeapProfile("some_file.prof")
to write information into a file. The output file can be later examined with go tool pprof some_file.prof
.
I am not sure how to detect when your application is out of memory. But you can probably put WriteHeapProfile
into defer
to launch it, when your app receives the kill signal from OS caused by the high memory usage.
Sources: