java jvm程序运行内存测试

free:此方法返回一个近似的总量为将来分配对象的当前可用内存

total:java虚拟机现在已经从操纵系统那里挖过来的内存大小

max:java虚拟机(这个进程)能构从操纵系统那里挖到的最大的内存

package Test;

public class Test {

    public static void main(String[] args) {	
    	System.out.println("free:" + Runtime.getRuntime().freeMemory() / 1024 / 1024);  
        System.out.println("total:" + Runtime.getRuntime().totalMemory() / 1024 / 1024);  
        System.out.println("max:" + Runtime.getRuntime().maxMemory() / 1024 / 1024);  
        System.out.println("=============");  
        try {  
            Thread.sleep(3000);  
        } catch (Exception ee) {  
            ee.printStackTrace();  
        }  
        String[] aaa = new String[2000000];  
    	System.out.println("free:" + Runtime.getRuntime().freeMemory() / 1024 / 1024);  
        System.out.println("total:" + Runtime.getRuntime().totalMemory() / 1024 / 1024);  
        System.out.println("max:" + Runtime.getRuntime().maxMemory() / 1024 / 1024);  
        System.out.println("=============");  
        try {  
            Thread.sleep(3000);  
        } catch (Exception ee) {  
            ee.printStackTrace();  
        }  
        for (int i = 0; i < 2000000; i++) {  
            aaa[i] = new String("aaa");  
        }  
    	System.out.println("free:" + Runtime.getRuntime().freeMemory() / 1024 / 1024);  
        System.out.println("total:" + Runtime.getRuntime().totalMemory() / 1024 / 1024);  
        System.out.println("max:" + Runtime.getRuntime().maxMemory() / 1024 / 1024);  
        System.out.println("=============");  
        try {  
            Thread.sleep(3000);  
        } catch (Exception ee) {  
            ee.printStackTrace();  
        } 
    }
}


你可能感兴趣的:(Java)