1 package org.simantics.modeling.tests.commands;
4 import java.io.IOException;
5 import java.lang.management.ManagementFactory;
6 import java.lang.reflect.InvocationTargetException;
7 import java.lang.reflect.Method;
9 import org.simantics.db.testing.common.CommandSequenceEnvironment;
10 import org.simantics.db.testing.common.WriteCommand;
12 public class WriteHeapDump extends WriteCommand<CommandSequenceEnvironment> {
14 private String fileName;
16 public WriteHeapDump(String fileName) {
17 this.fileName = fileName;
21 public void run(CommandSequenceEnvironment environment) throws Exception {
23 final File dumpFile = new File(fileName);
24 if (dumpFile.exists() && !dumpFile.delete()) {
28 Object bean = getBean();
32 Method m = bean.getClass().getMethod("dumpHeap", String.class, boolean.class);
33 m.invoke(bean, dumpFile.getAbsolutePath(), true);
34 } catch (IllegalArgumentException e) {
35 throw new InvocationTargetException(e);
36 } catch (IllegalAccessException e) {
37 throw new InvocationTargetException(e);
38 } catch (SecurityException e) {
39 throw new InvocationTargetException(e);
40 } catch (NoSuchMethodException e) {
41 throw new InvocationTargetException(e);
46 private static Object getBean() {
47 Class<?> beanClass = getBeanClass();
48 if (beanClass == null)
51 Object bean = ManagementFactory.newPlatformMXBeanProxy(
52 ManagementFactory.getPlatformMBeanServer(),
53 "com.sun.management:type=HotSpotDiagnostic",
56 } catch (IOException e) {
61 private static Class<?> getBeanClass() {
63 Class<?> clazz = Class.forName("com.sun.management.HotSpotDiagnosticMXBean");
65 } catch (ClassNotFoundException e) {