我有一个运行在Google App Engine标准环境上的Java应用程序,我想通过进行堆转储来分析内存使用情况。
是否有任何工具可以帮助完成此任务?
我知道 HotSpotDiagnosticMXBean 可以执行此操作,但是它将把文件写到我无法在应用程序引擎上访问的本地文件系统。有什么方法可以将转储流传输到云存储吗?
答案 0 :(得分:0)
目前尚无办法在App Engine Standard上进行堆转储,但App Engine团队目前正在研究this feature request。
请注意,此请求没有ETA。更新将发布在问题跟踪链接上。
答案 1 :(得分:0)
现在可以使用Appengine Standard写入临时文件: https://cloud.google.com/appengine/docs/standard/java11/using-temp-files, 使您能够以编程方式创建堆转储并将其保存到本地文件。拥有文件后,您可以将其发送到Google Cloud Storage,例如,然后从那里进行分析。 您可以使用 HotSpotDiagnosticMXBean 。这是一个代码段:
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);
}
}
}