zoukankan      html  css  js  c++  java
  • 监控JVM内存使用情况,剩余空间小于2M时报警

    一个简单的类,用来监控JVM内存使用情况,剩余空间小于2M时报警。

    复制代码
    import java.lang.management.ManagementFactory;
    import java.lang.management.OperatingSystemMXBean;
    import java.lang.reflect.Method;
    
    /**
     * Test
     */
    public class Test {
    
        public static void main(String[] args) throws InterruptedException {
            Runtime runtime = Runtime.getRuntime();
            while (true) {
                long freeMemory = runtime.freeMemory();
                long totalMemory = runtime.totalMemory();
                long maxMemory = runtime.maxMemory();
                String msg = "max:" + (maxMemory / 1024 / 1024) + "M,total:" + (totalMemory / 1024 / 1024) + "M,used:"
                        + ((totalMemory / 1024 / 1024) - (freeMemory / 1024 / 1024)) + "M,free:"
                        + (freeMemory / 1024 / 1024) + "M";
        
                boolean ok = (maxMemory - (totalMemory - freeMemory) > 2048); // 剩余空间小于2M报警
                if (!ok) {
                    System.out.println(msg);
                    System.err.println("剩余空间小于2M报警");
                }
                Thread.currentThread().sleep(1 * 1000);
            }
        }
    
    }
    复制代码
  • 相关阅读:
    Design Pattern
    javascript summary
    nodejs template
    MVC---Case 1
    About js
    本地schemeApp扩展
    BNU4208:Bubble sort
    [置顶] think in java interview-高级开发人员面试宝典代码示例
    java+socket 简易聊天工具
    oracle 字段自增 两段代码搞定
  • 原文地址:https://www.cnblogs.com/chinaifae/p/10239031.html
Copyright © 2011-2022 走看看