@@ -0,0 +1,2788 @@
+/*%0A * Copyright 2016 Red Hat, Inc. and/or its affiliates.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *       http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0Apackage org.drools.core.util;%0A%0Aimport javax.management.MBeanServer;%0Aimport java.lang.management.ManagementFactory;%0Aimport java.lang.reflect.Method;%0A%0A%0A@SuppressWarnings(%22restriction%22)%0Apublic class HeapDump %7B%0A    // This is the name of the HotSpot Diagnostic MBean%0A    private static final String HOTSPOT_BEAN_NAME =%0A            %22com.sun.management:type=HotSpotDiagnostic%22;%0A%0A    // field to store the hotspot diagnostic MBean%0A    private static volatile Object hotspotMBean;%0A%0A    /**%0A     * Call this method from your application whenever you%0A     * want to dump the heap snapshot into a file.%0A     *%0A     * @param fileName name of the heap dump file%0A     * @param live flag that tells whether to dump%0A     *             only the live objects%0A     */%0A    public static void dumpHeap(String fileName, boolean live) %7B%0A        // initialize hotspot diagnostic MBean%0A        initHotspotMBean();%0A        try %7B%0A            Class clazz = Class.forName(%22com.sun.management.HotSpotDiagnosticMXBean%22);%0A            Method m = clazz.getMethod(%22dumpHeap%22, String.class, boolean.class);%0A            m.invoke( hotspotMBean , fileName, live);%0A        %7D catch (RuntimeException re) %7B%0A            throw re;%0A        %7D catch (Exception exp) %7B%0A            throw new RuntimeException(exp);%0A        %7D%0A    %7D%0A%0A    // initialize the hotspot diagnostic MBean field%0A    private static void initHotspotMBean() %7B%0A        if (hotspotMBean == null) %7B%0A            synchronized (HeapDump.class) %7B%0A                if (hotspotMBean == null) %7B%0A                    hotspotMBean = getHotspotMBean();%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    // get the hotspot diagnostic MBean from the%0A    // platform MBean server%0A    private static Object getHotspotMBean() %7B%0A        try %7B%0A            Class clazz = Class.forName(%22com.sun.management.HotSpotDiagnosticMXBean%22);%0A            MBeanServer server = ManagementFactory.getPlatformMBeanServer();%0A            return ManagementFactory.newPlatformMXBeanProxy( server, HOTSPOT_BEAN_NAME, clazz );%0A        %7D catch (RuntimeException re) %7B%0A            throw re;%0A        %7D catch (Exception exp) %7B%0A            throw new RuntimeException(exp);%0A        %7D%0A    %7D%0A%7D

add test utility class to programmatically dump JVM heap