You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HeapDump.java 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.stress;
  16. import java.io.IOException;
  17. import java.lang.management.ManagementFactory;
  18. import java.lang.reflect.InvocationTargetException;
  19. import java.lang.reflect.Method;
  20. import com.sun.management.HotSpotDiagnosticMXBean;
  21. import org.apache.poi.util.SuppressForbidden;
  22. @SuppressForbidden("class only exists for manual tests in XSSFFileHandler")
  23. public class HeapDump {
  24. // This is the name of the HotSpot Diagnostic MBean
  25. private static final String HOTSPOT_BEAN_NAME =
  26. "com.sun.management:type=HotSpotDiagnostic";
  27. // field to store the hotspot diagnostic MBean
  28. private static volatile HotSpotDiagnosticMXBean hotspotMBean;
  29. /**
  30. * Call this method from your application whenever you
  31. * want to dump the heap snapshot into a file.
  32. *
  33. * @param fileName name of the heap dump file
  34. * @param live flag that tells whether to dump
  35. * only the live objects
  36. */
  37. public static void dumpHeap(String fileName, boolean live) throws IOException {
  38. try {
  39. if (isIbmVm()) {
  40. dumpHeapJ9(fileName);
  41. } else {
  42. // initialize hotspot diagnostic MBean
  43. initHotspotMBean();
  44. dumpHeapHotSpot(fileName, live);
  45. }
  46. } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
  47. throw new RuntimeException(e);
  48. }
  49. }
  50. // initialize the hotspot diagnostic MBean field
  51. private static void initHotspotMBean() throws IOException {
  52. if (hotspotMBean == null) {
  53. synchronized (HeapDump.class) {
  54. if (hotspotMBean == null) {
  55. hotspotMBean = getHotspotMBean();
  56. }
  57. }
  58. }
  59. }
  60. // get the hotspot diagnostic MBean from the platform MBean server
  61. private static HotSpotDiagnosticMXBean getHotspotMBean() throws IOException {
  62. return ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(),
  63. HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class);
  64. }
  65. private static boolean isIbmVm() {
  66. try {
  67. Class.forName("com.ibm.jvm.Dump");
  68. return true;
  69. } catch (ClassNotFoundException e) {
  70. return false;
  71. }
  72. }
  73. private static void dumpHeapJ9(String fileName) throws ClassNotFoundException, NoSuchMethodException,
  74. InvocationTargetException, IllegalAccessException {
  75. Class<?> dump = Class.forName("com.ibm.jvm.Dump");
  76. Method heapDumpToFile = dump.getMethod("heapDumpToFile", String.class);
  77. heapDumpToFile.invoke(dump, fileName);
  78. }
  79. private static void dumpHeapHotSpot(String fileName, boolean live) throws NoSuchMethodException,
  80. InvocationTargetException, IllegalAccessException {
  81. Method dumpHeap = hotspotMBean.getClass().getMethod("dumpHeap", String.class, boolean.class);
  82. dumpHeap.invoke(hotspotMBean, fileName, live);
  83. }
  84. }