-
-
Save charleehu/1382060 to your computer and use it in GitHub Desktop.
HotSpot VM utils
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GCUtils { | |
private static final String DISABLE_EXPLICIT_GC = "DisableExplicitGC"; // need to be "manageable" in VM | |
public static void forceGC() { | |
String oldValue = HotSpotUtils.getVMOption(DISABLE_EXPLICIT_GC); | |
if ("true".equals(oldValue)) { | |
HotSpotUtils.setVMOption(DISABLE_EXPLICIT_GC, "false"); | |
System.gc(); | |
HotSpotUtils.setVMOption(DISABLE_EXPLICIT_GC, oldValue); | |
} else { | |
System.gc(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.management.ManagementFactory; | |
import javax.management.MBeanServer; | |
import com.sun.management.HotSpotDiagnosticMXBean; | |
// import com.sun.management.VMOption; | |
public class HotSpotUtils { | |
public static String getVMOption(String name) { | |
return SingletonHolder.HOTSPOT_BEAN.getVMOption(name).getValue(); // could throw exception | |
} | |
public static void setVMOption(String name, String value) { | |
SingletonHolder.HOTSPOT_BEAN.setVMOption(name, value); | |
} | |
private static class SingletonHolder { | |
static final HotSpotDiagnosticMXBean HOTSPOT_BEAN = populateHotSpotBean(); | |
private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; | |
private static final HotSpotDiagnosticMXBean populateHotSpotBean() { | |
try { | |
MBeanServer server = ManagementFactory.getPlatformMBeanServer(); | |
HotSpotDiagnosticMXBean bean = ManagementFactory.newPlatformMXBeanProxy( | |
server, HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class); | |
return bean; | |
} catch (java.io.IOException e) { | |
throw new InternalError("should run on a HotSpot VM"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment