标签:sans 而在 生产 没有 val sub pre 路由 阶段
public class GCUtil { /** * 调用 System.gc */ public static void systemGC() { long startTime = System.currentTimeMillis(); LOGGER.info("Call for system gc start..."); System.gc(); LOGGER.info("Call for system gc end, spend {}ms", System.currentTimeMillis() - startTime); } /** * 保证当前年轻代进入老年代的 gc,用于模型更新以及刚上线期间 */ public static void youngPromoteGC() { try { long startTime = System.currentTimeMillis(); LOGGER.info("Call for young promote gc start..."); RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean(); List<String> args = runtimeMXBean.getInputArguments(); // 获取 eden 区大小 long edenMSize = 0; for (String arg : args) { LOGGER.info("--------------- Jvm param: {}", arg); if (arg.startsWith(JVM_XMN)) { String edenSizeStr = arg.substring(JVM_XMN.length()); if (edenSizeStr.toLowerCase().endsWith("g")) { long edenGSize = Long.valueOf(edenSizeStr.substring(0, edenSizeStr.length() - 1)); edenMSize = edenGSize * 1024; } else if (edenSizeStr.toLowerCase().endsWith("m")) { edenMSize = Long.valueOf(edenSizeStr.substring(0, edenSizeStr.length() - 1)); } else { LOGGER.warn("Cannot recognize -Xmn argument: " + JVM_XMN); } } } if (edenMSize == 0) { edenMSize = getEdenMemorySize(); } if (edenMSize <= 0) { LOGGER.warn("Extract jvm -Xmn failed"); return; } // 获取 晋升老年代最大次数 int tenuringThreshold = DEFAULT_TENURING_THRESHOLD; for (String arg : args) { if (arg.startsWith(MAX_TENURING_THRESHOLD)) { String tenuringThresholdStr = arg.substring(MAX_TENURING_THRESHOLD.length() + 1); tenuringThreshold = Integer.valueOf(tenuringThresholdStr); if (tenuringThreshold > DEFAULT_TENURING_THRESHOLD) { tenuringThreshold = DEFAULT_TENURING_THRESHOLD; } } } LOGGER.info("Start to young gc, -Xmn={}m, -XX:MaxTenuringThreshold={}", edenMSize, tenuringThreshold); // 手动触发 Young GC for (int i = 0; i < edenMSize * tenuringThreshold; ++i) { allocate_1M(); } // System GC,清理老年代 System.gc(); LOGGER.info("Call for young promote gc end, spend {}ms", System.currentTimeMillis() - startTime); } catch (Exception e) { LOGGER.error("Trigger young promote gc failed: ", e); } } /** * 获取新生代大小,单位 M */ private static long getEdenMemorySize() { List<MemoryPoolMXBean> poolMXBeanList = ManagementFactory.getMemoryPoolMXBeans(); for (MemoryPoolMXBean memoryPoolMXBean : poolMXBeanList) { if (memoryPoolMXBean.getName().toLowerCase().contains("eden")) { long maxUsage = memoryPoolMXBean.getUsage().getMax(); return maxUsage >> 20; } } return -1; } /** * 生成个 1M 对象 */ private static void allocate_1M() { byte[] _1M = new byte[1024 * 1024]; } private GCUtil() { } private static final String MAX_TENURING_THRESHOLD = "-XX:MaxTenuringThreshold"; private static final int DEFAULT_TENURING_THRESHOLD = 15; private static final String JVM_XMN = "-Xmn"; private static final Logger LOGGER = LoggerFactory.getLogger(GCUtil.class); }
Java 代码监控 JVM 运行状态 —— 记一次 JVM 调优的毛招
标签:sans 而在 生产 没有 val sub pre 路由 阶段
原文地址:https://www.cnblogs.com/43726581Gavin/p/9650778.html