Java测试当前应用所占用的内存示例

时间:2021-12-06 17:05:50
package test; import java.util.HashMap; import java.util.Map; public class TestMemory { public static void main(String[] args) { System. gc(); long total = Runtime. getRuntime(). totalMemory(); // byte long m1 = Runtime. getRuntime(). freeMemory(); System. out. println( "before:" + (total - m1)); Map<Double,Double> map = new HashMap<Double,Double>(); for( int i= 0; i < 1e6; i++){ map. put( new Double(Math. random()* 1e9), new Double(Math. random()* 1e10)); } long total1 = Runtime. getRuntime(). totalMemory(); long m2 = Runtime. getRuntime(). freeMemory(); System. out. println ( "after:" + (total1 - m2)); // System.out.println(map.toString()); System. exit( 0); } }