如何判断Java程序';运行时内存的动态使用?

如何判断Java程序';运行时内存的动态使用?,java,jvm,jmx,Java,Jvm,Jmx,定义Java类时,我们可以在堆中获取对象的内存使用情况,但由于程序的未知行为,我们无法获取运行时内存使用情况。例如,以下类: public class Sample{ private int age; private String name; private static List scores = new ArrayList<Integer>(); public static void main(String[] args){ Scanner sc

定义Java类时,我们可以在堆中获取对象的内存使用情况,但由于程序的未知行为,我们无法获取运行时内存使用情况。例如,以下类:

public class Sample{
   private int age;
   private String name;
   private static List scores = new ArrayList<Integer>();
   public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
     while(sc.hasNext()){
       scores.add(sc.nextInt());
     }
   }
}
公共类示例{
私人互联网;
私有字符串名称;
私有静态列表分数=新建ArrayList();
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
while(sc.hasNext()){
添加(sc.nextInt());
}
}
}

那么,我们如何获得类示例的运行时内存使用率呢?

有多种工具可以实现这一点。您可以使用JDK中的jvisualvm。或者,也有一些商业的。如需了解更多信息,请遵循以下步骤。此外,您可以在jvisualvm上安装“visual GC”插件,该插件显示堆的不同部分的更改,例如:伊甸园、幸存者、旧世代、元空间。甚至执行GC的时间和加载类的时间也是可以跟踪的


从工具->插件在jvisualvm上安装插件

有多种工具可以实现这一点。您可以使用JDK中的jvisualvm。或者,也有一些商业的。如需了解更多信息,请遵循以下步骤。此外,您可以在jvisualvm上安装“visual GC”插件,该插件显示堆的不同部分的更改,例如:伊甸园、幸存者、旧世代、元空间。甚至执行GC的时间和加载类的时间也是可以跟踪的


从工具->插件在jvisualvm上安装插件

您可以获得另一个java参数,如“已用内存”,如下所示:

public class Sample{
   private int age;
   private String name;
   private static List scores = new ArrayList<Integer>();
   public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
     while(sc.hasNext()){
       scores.add(sc.nextInt());
     }

        Runtime runtime = Runtime.getRuntime();

        System.out.println("Used Memory:" 
            + (runtime.totalMemory() - runtime.freeMemory()));

        System.out.println("Free Memory:" 
            + runtime.freeMemory());

        System.out.println("Total Memory:" + runtime.totalMemory());

        System.out.println("Max Memory:" + runtime.maxMemory());
   }
}
公共类示例{
私人互联网;
私有字符串名称;
私有静态列表分数=新建ArrayList();
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
while(sc.hasNext()){
添加(sc.nextInt());
}
Runtime=Runtime.getRuntime();
System.out.println(“已用内存:”
+(runtime.totalMemory()-runtime.freemory());
System.out.println(“可用内存:”
+runtime.freemory());
System.out.println(“总内存:+runtime.totalMemory());
System.out.println(“最大内存:+runtime.maxMemory());
}
}

如果你想计算一个类的使用率,你必须在运行之前和之后得到这个参数,然后比较这些数字,你可以得到另一个java参数,比如“已用内存”,如下所示:

public class Sample{
   private int age;
   private String name;
   private static List scores = new ArrayList<Integer>();
   public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
     while(sc.hasNext()){
       scores.add(sc.nextInt());
     }

        Runtime runtime = Runtime.getRuntime();

        System.out.println("Used Memory:" 
            + (runtime.totalMemory() - runtime.freeMemory()));

        System.out.println("Free Memory:" 
            + runtime.freeMemory());

        System.out.println("Total Memory:" + runtime.totalMemory());

        System.out.println("Max Memory:" + runtime.maxMemory());
   }
}
公共类示例{
私人互联网;
私有字符串名称;
私有静态列表分数=新建ArrayList();
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
while(sc.hasNext()){
添加(sc.nextInt());
}
Runtime=Runtime.getRuntime();
System.out.println(“已用内存:”
+(runtime.totalMemory()-runtime.freemory());
System.out.println(“可用内存:”
+runtime.freemory());
System.out.println(“总内存:+runtime.totalMemory());
System.out.println(“最大内存:+runtime.maxMemory());
}
}

如果你想计算一个类的使用率,你必须在运行之前和之后得到这个参数,然后比较这些数字

这里列出了一些技巧:这里列出了一些技巧: