----------~开篇分享一句话:【纸上得来终觉浅,绝知此事要躬行】~---------------------------------------
String.intern() 方法会自动把String放到jvm的PSPermGen的常量区。
关于String.intern()的使用需要注意以下两点:
1、对于日常工作中List中的数据对象,如果对象的某个属性是String,比如性别,国家等重复率较高的字符串取值,如果放入常量区,会节省大量的内存空间。
2、jvm的常量池的的搜索比较慢,速度甚至比ConcurrentHashMap 慢了不少。
证明:
JVM参数配置:
-Xmx200M -Xms200M -Xmn100M -XX:SurvivorRatio=8 -XX:+PrintGCDetails
public static long times = 10000000L;
public static void testIntern() {
System.gc();
List<String> list = new ArrayList<String>();
long l = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
list.add(("A" + (i % 1000)).intern());
}
long ll = System.currentTimeMillis();
System.out.println("testIntern time :" + (ll -l));
System.gc();
System.out.println("testIntern:"
+ (wrapM(Runtime.getRuntime().totalMemory()) - wrapM(Runtime
.getRuntime().freeMemory())));
}
结果:
testIntern time :2657
testIntern memory:1.8076171875M
eden space 86272K, 3% used [0x00000000f9c00000,0x00000000f9f4b3f0,0x00000000ff040000)
public static void testCommon() {
System.gc();
List<String> list = new ArrayList<String>();
long l = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
list.add(("A" + (i % 1000)));
}
long ll = System.currentTimeMillis();
System.out.println("testIntern time :" + (ll -l));
System.gc();
System.out.println("testCommon memory:"
+ (wrapM(Runtime.getRuntime().totalMemory()) - wrapM(Runtime
.getRuntime().freeMemory())));
}
结果:
Exception in thread "main" [Full GC [PSYoungGen: 81920K->0K(92160K)] [PSOldGen: 102400K->188K(102400K)] 184320K->188K(194560K) [PSPermGen: 3048K->3048K(21248K)], 0.0530156 secs] [Times: user=0.05 sys=0.00, real=0.05 secs]
java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.lang.StringBuilder.toString(StringBuilder.java:430)
at com.mystore.core.common.TestMemory.testCommon(TestMemory.java:85)
at com.mystore.core.common.TestMemory.main(TestMemory.java:13)
public static void testCurrentHashMap() {
System.gc();
List<String> list = new ArrayList<String>();
long l = System.currentTimeMillis();
for (int i = 0; i < times; i++) {
list.add((StringCache.get("A" + (i % 1000))));
}
long ll = System.currentTimeMillis();
System.out.println("testIntern time :" + (ll -l));
System.gc();
System.out.println("testCurrentHashMap memory:"
+ (wrapM(Runtime.getRuntime().totalMemory()) - wrapM(Runtime
.getRuntime().freeMemory())));
}
private static double wrapM(long length) {
return length / 1024 / 1024.0;
}
static class StringCache {
private static ConcurrentHashMap<String, String> map = new ConcurrentHashMap<String, String>(10000);
public static String get(String str){
if(null == str){
return null;
}
String ret = map.get(str);
if(null == ret){
map.putIfAbsent(str, str);
ret = map.get(str);
}
return ret;
}
}
结果:testIntern time :2006
testCurrentHashMap memory:1.9482421875
eden space 85888K, 3% used [0x00000000f9c00000,0x00000000f9f477f8,0x00000000fefe0000)
结论:
对比testCommon 和 testIntern 说明testCommon 会占用较多的堆区内存,testIntern 会导致常量区会有微量的增长(仅仅1000个字符常量而已)
对比testIntern 和 testCurrentHashMap ,testCurrentHashMap 在性能方面有优势,更为需要关注的是testCurrentHashMap的内存分配在了堆区,而testIntern 分配在了常量区,一般情况下 堆区的老年代要比持久代要大的多,所以从gc的角度来说,更应该使用testCurrentHashMap 的方式。不好的一点是 testCurrentHashMap 中的常量会一直增长没有过期 策略,而常量池则会在full gc 的时候自动做清理。testCurrentHashMap 优化的方向是 使用带缓存并且线程安全的Map,比如guava的缓存Map
参考: 性能对比参见
http://stackoverflow.com/questions/10624232/performance-penalty-of-string-intern