这几天都在外面跑,没时间上机调代码,晚上回来趁这机会研究下,结果如下:
bobguo@bobguo-PC:~/source/eetest$ java -version && javac hash.java && cat hash.java && time java -Xms2G -verbose:gc hash && time java -verbose:gc hash
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=gasp
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=gasp
import java.util.HashMap;
import java.util.Map;
public class hash {
public static void main(String[] args) {
long start = System.currentTimeMillis();
String time;
Map<String, String> map = new HashMap<>();
for (int i = 0; i < 1000000; i++) {
time = String.valueOf(System.currentTimeMillis());
map.put(i + "_" + time, time);
}
System.out.println(System.currentTimeMillis() - start);
}
}
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=gasp
690
real 0m0.806s
user 0m0.964s
sys 0m0.104s
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=gasp
[GC (Allocation Failure) 31744K->16720K(121856K), 0.0235111 secs]
[GC (Allocation Failure) 48464K->35592K(153600K), 0.0363266 secs]
[GC (Allocation Failure) 99080K->96188K(160256K), 0.0982848 secs]
[Full GC (Ergonomics) 96188K->95872K(278528K), 1.0974248 secs]
[GC (Allocation Failure) 159091K->145848K(295424K), 0.1148326 secs]
[Full GC (Ergonomics) 145848K->145669K(398336K), 0.9425082 secs]
2901
real 0m3.010s
user 0m9.548s
sys 0m0.200s
这个场景特殊在哪里呢?特殊在java擅长长期运行,所以针对这种从0开始剧烈使用内存,然后直接结束的的场景,默认参数并没有很好的优化导致的。