????Java 6??7??8?е?String.intern–???????
?????????????????Java 6??????????String.intern???????????????????Java 7???Java 8????????Щ??????
???????????
????????????????????????????????????????String????????????????????????????????????????????????????Map<String??String>????????????weak???????soft??????????map?е??????????????????????????????????JDK????String.intern()??
??????????????Java 6?????String.intern()???????????ó????????????к?????????OutOfMemoryException??Oracle Java 7??????????????????????????????μ?????????http://bugs.sun.com/view_bug.do?bug_id=6962931???http://bugs.sun.com/view_bug.do?bug_id=6962930
????Java 6?е?String.intern()
????????????????й????String?????洢??PermGen??—???й????С????????????洢??????????????????????????????????????PermGen???????????????г???????ù??????????????????????ù???????????????????????δ??????????????????ж?????κγ??????????????
????Java 6?????????????????????λ??—PermGen??PermGen???С???????????????????????????????????-XX:MaxPermSize=N???????????????С???????????????????????PermGen??С??32M??96M????????????????С????????С????????????????????????????String.intern???????С??—??ò????????????intern?κ?????????????????????????JAVA6?д?????????????Map????????????
????Java 7?е?String.intern()
????Java 7??Oracle????????????????????????????—????????λ?????????heap?????????ζ???????????????????????????????е????????????????heap?????????????????????????????????????????????????С????????????????????????????????????????Java 7?????String.intern()??
????????????е?????????????
??????????JVM????????е??????????????????????????Щ????????????κ????á????????????а汾??Java??????ζ?????interned???????????????????????κ?????—???????JVM??????????б???????????
??????????????λ????????????????????JVM???????????????????????????λ???????????????—Υ???????????????????????????????????????????д????????????檔???????????????????????????????????????????????????????????????????????????
??????Java 6??7??8??JVM???????????
?????????????????????й????????HashMap????????????????hash?????????б???Щ???????????Java bug?????л??http://bugs.sun.com/view_bug.do?bug_id=6962930
??????????С??1009(??????????????bug?????????У???Java7u40????????)????JAVA 6????汾???????????????????java6u30??java6u41?е????????????????java 7???????????????????????java7u02???????????????????????????-XX:StringTableSize=N??N?????????Map???С????????????????????????????С??
??????Java 6????????????????????????????α???????????PermGen????С?С??????????????????Java 6
????Java 7?????Java7u40??
??????Java7?У????仰????????????????????????С?????ζ?????????????ú?String????С????????????????ó???????????????????????????????????涼???????????????????????£?????????100??????????????????????8-16M????濴???????????????????1??000??000???-XX:StringTaleSize???–???????????????1??000??003???棩
????????????????String??Map?е????—???????????????HashCode???????????顣
?????????????????????-XX:StringTalbeSize?(????????1009)??????????????????String.intern()—????????????????????0?????С????
??????????????internС??100????????????????????????????????????50???????????????????????????????????100???????????????????????????
???????????????С????ó??????????????????intern??????????????????intern 10??000??????????е??????
????0;time=0.0 sec
????50000;time=0.03 sec
????100000;time=0.073 sec
????150000;time=0.13 sec
????200000;time=0.196 sec
????250000;time=0.279 sec
????300000;time=0.376 sec
????350000;time=0.471 sec
????400000;time=0.574 sec
????450000;time=0.666 sec
????500000;time=0.755 sec
????550000;time=0.854 sec
????600000;time=0.916 sec
????650000;time=1.006 sec
????700000;time=1.095 sec
????750000;time=1.273 sec
????800000;time=1.248 sec
????850000;time=1.446 sec
????900000;time=1.585 sec
????950000;time=1.635 sec
????1000000;time=1.913 sec
????????????Core i5-3317U@1.7Ghz CPU?豸????е????????????????????????????????JVM???????????????????????????????????????intern 5000????????????????????д?????????????ó????????????
?????????????-XX:StringTableSize=100003?????????????в????
????50000;time=0.017 sec
????100000;time=0.009 sec
????150000;time=0.01 sec
????200000;time=0.009 sec
????250000;time=0.007 sec
????300000;time=0.008 sec
????350000;time=0.009 sec
????400000;time=0.009 sec
????450000;time=0.01 sec
????500000;time=0.013 sec
????550000;time=0.011 sec
????600000;time=0.012 sec
????650000;time=0.015 sec
????700000;time=0.015 sec
????750000;time=0.01 sec
????800000;time=0.01 sec
????850000;time=0.011 sec
????900000;time=0.011 sec
????950000;time=0.012 sec
????1000000;time=0.012 sec
??????????????????????????????????????????Map????????б?????????????????????10????????????????????????????????????????в???1000??????????????ζ??Map?е???????б????????100?????????
????2000000;time=0.024 sec
????3000000;time=0.028 sec
????4000000;time=0.053 sec
????5000000;time=0.051 sec
????6000000;time=0.034 sec
????7000000;time=0.041 sec
????8000000;time=0.089 sec
????9000000;time=0.111 sec
????10000000;time=0.123 sec
???????????????????С?????100??????????1??000??003??
????1000000;time=0.005 sec
????2000000;time=0.005 sec
????3000000;time=0.005 sec
????4000000;time=0.004 sec
????5000000;time=0.004 sec
????6000000;time=0.009 sec
????7000000;time=0.01 sec
????8000000;time=0.009 sec
????9000000;time=0.009 sec
????10000000;time=0.009 sec
?????????????????????????????????“0??100??”???????????????????С?????????£????????????????1??000??000?????????
????????????????????????????
??????????????????JVM????????WeakHashMap<String??WeakReference<String>>?????????????JVM?????????????????????滻String.intern??
????private static final WeakHashMap<String??WeakReference<String>>s_manualCache=
????new WeakHashMap<String??WeakReference<String>>(100000);
????private static String manualIntern(final String str)
????{
????final WeakReference<String>cached=s_manualCache.get(str);
????if(cached!=null)
????{
????final String value=cached.get();
????if(value!=null)
????return value;
????}
????s_manualCache.put(str??new WeakReference<String>(str));
????return str;
????}