1

I have an Java application running on Google App Engine Standard Environment for which I want to analyse memory usage by taking heap dump.

Are there any tools which can help in this task?

I know of HotSpotDiagnosticMXBean which can do this, but it will write the file to the local filesystem which I don't have access to on app engine. Is there any way to stream the dump to cloud storage?

Kishu Agarwal
  • 378
  • 3
  • 8

2 Answers2

0

There is no way to get heap dump on App Engine Standard for now but the App Engine Team is currently looking into this feature request.

Please note that there are not ETA's for this request. Updates will be posted on the issue tracker link.

Yasser Karout
  • 336
  • 1
  • 4
  • Thanks for the response @Yasser. Yeah, I only raised that feature request in the hope that they might add this feature. – Kishu Agarwal Nov 21 '18 at 08:40
0

There is now a possibility to write to a temporary file with Appengine Standard: https://cloud.google.com/appengine/docs/standard/java11/using-temp-files, enabling you to create a heap dump programatically and save it to a local file. Once you have the file you can send it to Google Cloud Storage, for example, and analyse it from there. You can use as you mentioned HotSpotDiagnosticMXBean. Here's a code snippet:

final MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
if (memory.getHeapMemoryUsage().getUsed() > 48 * MB) {
    final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    final HotSpotDiagnosticMXBean mxBean = ManagementFactory.newPlatformMXBeanProxy(server, "com.sun.management:type=HotSpotDiagnostic", HotSpotDiagnosticMXBean.class);

    final String fileName = "/tmp/heap.hprof";

    mxBean.dumpHeap(fileName, true);

    final GcsFilename objectId = new GcsFilename("my_bucket", "heap-dump.hprof");
    final GcsOutputChannel output = gcs.createOrReplace(objectId, options);

    try (final InputStream is = new FileInputStream(fileName);
         final OutputStream os = Channels.newOutputStream(output)) {
        final byte[] buffer = new byte[65536];

        for (int read; (read = is.read(buffer)) > -1; ) {
            os.write(buffer, 0, read);
        }
    }
}
Roxana Roman
  • 1,012
  • 1
  • 8
  • 17
  • That's great. But since it's going to use the instance memory, it might not work if the size of the heap dump is more. Unless App engine provides inbuilt support for taking heap dump, our options are limited. – Kishu Agarwal Oct 30 '19 at 10:41