1. The test java code:
package com.haowei.oom; import com.haowei.Util; import java.util.ArrayList; public class OOM { public static void stringInternOOM(){ int counter = 1; try{ int max = Integer.MAX_VALUE; ArrayList list = new ArrayList(); for(int i=0;i<max;i++){ String str = String.valueOf(i); list.add(str.intern()); counter++; } }catch(Exception e){ e.printStackTrace(); }finally{ Util.log("the max String.Intern counter = " + counter); } } public static void main(String args[]){ stringInternOOM(); } }
2. The execute script:
java -cp . -Xms16m -Xmx16m -XX:PermSize=16m -XX:MaxPermSize=16m com/haowei/oom/OOM
3. The result of JDK1.6.0
*********version begin**************** Apache Ant(TM) version 1.8.2 compiled on December 20 2010 java version "1.6.0_26" Java(TM) SE Runtime Environment (build 1.6.0_26-b03) Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing) *********version end**************** *************** begin to run java *************** the max String.Intern counter = 348980 Exception in thread "main" java.lang.OutOfMemoryError: PermGen space at java.lang.String.intern(Native Method) at com.haowei.oom.OOM.stringInternOOM(Unknown Source) at com.haowei.oom.OOM.main(Unknown Source)
4.The result of JDK1.7.0
*********version begin**************** Apache Ant(TM) version 1.8.2 compiled on December 20 2010 openjdk version "1.7.0-internal-debug" OpenJDK Runtime Environment (build 1.7.0-internal-debug-haowei_2012_02_06_22_42-b00) OpenJDK Client VM (build 21.0-b17-jvmg, mixed mode) *********version end**************** *************** begin to run java *************** the max String.Intern counter = 304460 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.lang.Integer.toString(Integer.java:333) at java.lang.String.valueOf(String.java:3007) at com.haowei.oom.OOM.stringInternOOM(Unknown Source) at com.haowei.oom.OOM.main(Unknown Source) VM option 'PermSize=16m' VM option 'MaxPermSize=16m'