java – 从应用程序内创建堆转储,不需要HotSpotDiagnosticMXBean

前端之家收集整理的这篇文章主要介绍了java – 从应用程序内创建堆转储,不需要HotSpotDiagnosticMXBean前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在我的应用程序内创建堆转储,而不使用HotSpotDiagnosticMXBean类.
由于 java / rt.jar的访问限制,我无法使用依赖关系对HotSpotDiagnosticMXBean进行编译.我知道如何解决eclipse.compiler错误,但是我如何解决它的构建?
有没有办法通过编程方式创建堆转储?

>这个解决方案不行,因为它是HotSpotDiagnosticMXBean依赖关系:HotSpot-dependent Heap Dump

解决方法

好的,好像你可以通过使用反射来绕过限制:
  1. package lab.heapdump;
  2.  
  3. import javax.management.MBeanServer;
  4. import java.lang.management.ManagementFactory;
  5. import java.lang.reflect.Method;
  6.  
  7.  
  8. @SuppressWarnings("restriction")
  9. public class HeapDump {
  10. // This is the name of the HotSpot Diagnostic MBean
  11. private static final String HOTSPOT_BEAN_NAME =
  12. "com.sun.management:type=HotSpotDiagnostic";
  13.  
  14. // field to store the hotspot diagnostic MBean
  15. private static volatile Object hotspotMBean;
  16.  
  17. /**
  18. * Call this method from your application whenever you
  19. * want to dump the heap snapshot into a file.
  20. *
  21. * @param fileName name of the heap dump file
  22. * @param live flag that tells whether to dump
  23. * only the live objects
  24. */
  25. static void dumpHeap(String fileName,boolean live) {
  26. // initialize hotspot diagnostic MBean
  27. initHotspotMBean();
  28. try {
  29. Class clazz = Class.forName("com.sun.management.HotSpotDiagnosticMXBean");
  30. Method m = clazz.getMethod("dumpHeap",String.class,boolean.class);
  31. m.invoke( hotspotMBean,fileName,live);
  32. } catch (RuntimeException re) {
  33. throw re;
  34. } catch (Exception exp) {
  35. throw new RuntimeException(exp);
  36. }
  37. }
  38.  
  39. // initialize the hotspot diagnostic MBean field
  40. private static void initHotspotMBean() {
  41. if (hotspotMBean == null) {
  42. synchronized (HeapDump.class) {
  43. if (hotspotMBean == null) {
  44. hotspotMBean = getHotspotMBean();
  45. }
  46. }
  47. }
  48. }
  49.  
  50. // get the hotspot diagnostic MBean from the
  51. // platform MBean server
  52. private static Object getHotspotMBean() {
  53. try {
  54. Class clazz = Class.forName("com.sun.management.HotSpotDiagnosticMXBean");
  55. MBeanServer server = ManagementFactory.getPlatformMBeanServer();
  56. Object bean =
  57. ManagementFactory.newPlatformMXBeanProxy(server,HOTSPOT_BEAN_NAME,clazz);
  58. return bean;
  59. } catch (RuntimeException re) {
  60. throw re;
  61. } catch (Exception exp) {
  62. throw new RuntimeException(exp);
  63. }
  64. }
  65.  
  66. public static void main(String[] args) {
  67. // default heap dump file name
  68. String fileName = "D:\\heap.bin";
  69. // by default dump only the live objects
  70. boolean live = true;
  71.  
  72. // simple command line options
  73. switch (args.length) {
  74. case 2:
  75. live = args[1].equals("true");
  76. case 1:
  77. fileName = args[0];
  78. }
  79.  
  80. // dump the heap
  81. dumpHeap(fileName,live);
  82. }
  83. }

猜你在找的Java相关文章