Todd

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            65 隨筆 :: 0 文章 :: 24 評論 :: 0 Trackbacks
          使用myeclipse啟動tomcat 報java heap space ,PermGen space 錯誤,分別為 heap內存不足,PermGen內存不足
          需加大 tomcat啟動項參數 Xmx 和 XX:MaxPermSize
          PermGen是指內存的永久保存區域,它用于存放class和 method 對象,以及String 對象
          (sun原文:permanent generation is the area of the heap where class and method objects are stored. If an application loads a very large number of classes, then the size of the permanent generation might need to be increased using the -XX:MaxPermSize option.
          Interned java.lang.String objects are also stored in the permanent generation. The java.lang.String class maintains a pool of strings. When the intern method is invoked, the method checks the pool to see if an equal string is already in the pool. If there is, then the intern method returns it; otherwise it adds the string to the pool. In more precise terms, the java.lang.String.intern method is used to obtain the canonical representation of the string; the result is a reference to the same class instance that would be returned if that string appeared as a literal. If an application interns a huge number of strings, the permanent generation might need to be increased from its default setting.
          When this kind of error occurs, the text String.intern or ClassLoader.defineClass might appear near the top of the stack trace that is printed.
          The jmap -permgen command prints statistics for the objects in the permanent generation, including information about internalized String instances. See 2.6.4 Getting Information on the Permanent Generation.

          PermGen又是一個特殊內存區域:Classloader 加載的東東是不能回收的,它們放在PermGen中
          (tomcat原文:Why does the memory usage increase when I redeploy a web application? Because the Classloader (and the Class objects it loaded) cannot be recycled. They are stored in the permanent heap generation by the JVM, and when you redepoy a new class loader is created, which loads another copy of all these classes. This can cause OufOfMemoryErrors eventually.)

          回到我的問題來,打開%java_home%bin\jvisualvm.exe  (jdk6以上有)
          查看tomcat的內存情況,點擊tomcat標簽下monitor , 當used heap = max heap      used PermGen = max PermGen 時tomcat還在啟動中,一會就報錯;
          由于此前已經將myeclipse中server->tomcat->tomcatx.x->jdk的參數設置成-Xms64m -Xmx512m -XX:MaxPermSize=80m
          但是monitor 畫面中max heap為 256M
          點擊overview標簽,發現jvm參數如下:
          -Xms64m
          -Xmx512m
          -XX:MaxPermSize=80m
          -Xms64m
          -Xmx256m

          顯然tomcat使用了最后的設置-Xms64m -Xmx256m;這個是在myeclipse的屬性-》java->installed jres 中的vm啟動參數里,雙擊默認啟用的jre,把參數去掉或者設置成-Xms64m -Xmx512m
          再回到server->tomcat->tomcatx.x->jdk中把-XX:MaxPermSize=80m修改為-XX:MaxPermSize=128m
          保存后再啟動tomcat,ok

          另外,使用jvisualvm持續監測tomcat內存情況發現heap區域的內存使用會在一個高峰后穩定降低,內存被回收了;
          而PermGen區域的內存則是不斷增加到達一個峰值后就不再增加,但之后此區的內存沒有被回收,驗證了上面的說法;

          多說兩句:java的動態加載衍生出諸多框架,在空間上也暴露出問題,反射在時間上也存在效率問題:下面是組測試數據:

          Java version 1.6.0_13
          Java HotSpot(TM) Client VM
          11.3-b02
          Sun Microsystems Inc.

          Direct access using member field:
           47 125 47 46 46
           average time = 66 ms.
          Reference access to member field:
           109 109 110 94 109
           average time = 106 ms.
          Reflection access to member field:
           13094 12984 13063 13062 13094
           average time = 13051 ms.

          Java version 1.6.0_13
          Java HotSpot(TM) Client VM
          11.3-b02
          Sun Microsystems Inc.

          Direct call using member field:
           47 31 109 109 31
           average time = 70 ms.
          Direct call using passed value:
           16 16 16 31 15
           average time = 20 ms.
          Call to object using member field:
           46 47 47 47 32
           average time = 43 ms.
          Call to object using passed value:
           15 16 31 16 16
           average time = 20 ms.
          Reflection call using member field:
           812 782 844 844 844
           average time = 829 ms.
          Reflection call using passed value:
           938 953 954 1031 953
           average time = 973 ms.

          Java version 1.6.0_13
          Java HotSpot(TM) Client VM
          11.3-b02
          Sun Microsystems Inc.

          Direct Object creation:
           62 47 78 32 93
           average time = 63 ms.
          Reflection Object creation:
           125 94 94 109 187
           average time = 121 ms.
          Direct byte[8] creation:
           125 187 94 172 94
           average time = 137 ms.
          Reflection byte[8] creation:
           266 171 187 188 219
           average time = 191 ms.
          Direct byte[64] creation:
           250 172 156 125 203
           average time = 164 ms.
          Reflection byte[64] creation:
           281 219 203 203 219
           average time = 211 ms.

          華麗的上層需要堅實的底層基礎

          http://wiki.apache.org/tomcat/FAQ/Deployment
          http://download.oracle.com/javase/7/docs/webnotes/tsg/TSG-VM/html/memleaks.html#gbyuu

          posted on 2011-10-18 22:16 Todd 閱讀(3667) 評論(1)  編輯  收藏 所屬分類: eclipsejava

          評論

          # re: java heap space, PermGen space 錯誤 使用jvisualvm監測設置合理值 2011-10-20 08:34 tb
          不錯 又收藏一個錯誤  回復  更多評論
            

          主站蜘蛛池模板: 武鸣县| 罗江县| 开原市| 甘肃省| 昌平区| 闽清县| 禹城市| 大埔县| 灵山县| 花垣县| 仁怀市| 蓬溪县| 博野县| 阿拉善右旗| 资源县| 盘山县| 吐鲁番市| 哈密市| 犍为县| 呼伦贝尔市| 高邑县| 华阴市| 弥渡县| 万载县| 靖州| 宜川县| 永丰县| 平凉市| 西平县| 镇雄县| 岱山县| 焦作市| 万宁市| 南郑县| 赤城县| 方山县| 平顶山市| 澄迈县| 梧州市| 鹿邑县| 乐安县|