7

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.

galusben
  • 5,948
  • 6
  • 33
  • 52
  • What platform and what is the definition of “out of memory”? If the Linux OOM-killer terminates the process, it’s with a SIGKILL, which you of course can’t do anything about. – JimB Mar 23 '20 at 01:42
  • Java has parameter HeapDumpOnOutOfMemoryError. I would love to have something like that. See here: https://docs.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/clopts.html – galusben Mar 23 '20 at 07:30
  • 2
    The JVM sets a fixed heap size; most natively compiled languages have no such feature. – JimB Mar 23 '20 at 12:48
  • @JimB thanks for your help. It is a multi platform app, I don’t want to rely on specific OS features. – galusben Mar 24 '20 at 06:27
  • 1
    Your app with either fail to allocate memory, in which case it will crash immediately, or the OS may kill it under memory pressure (which may not be because of your app). Either way there's no way to do anything once you're exhausted the available memory. All you could do is watch for an allocated threshold and write the heapdump yourself. – JimB Mar 24 '20 at 17:24

1 Answers1

1

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:

  1. Package runtime/pprof
  2. Profiling Go Programs
  3. How I investigated memory leaks in Go using pprof on a large codebase
  4. Heat dump in Go using pprof
marek
  • 71
  • 5