Todd

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            65 隨筆 :: 0 文章 :: 24 評論 :: 0 Trackbacks
          使用myeclipse啟動tomcat 報java heap space ,PermGen space 錯誤,分別為 heap內(nèi)存不足,PermGen內(nèi)存不足
          需加大 tomcat啟動項參數(shù) Xmx 和 XX:MaxPermSize
          PermGen是指內(nèi)存的永久保存區(qū)域,它用于存放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又是一個特殊內(nèi)存區(qū)域: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的內(nèi)存情況,點擊tomcat標簽下monitor , 當used heap = max heap      used PermGen = max PermGen 時tomcat還在啟動中,一會就報錯;
          由于此前已經(jīng)將myeclipse中server->tomcat->tomcatx.x->jdk的參數(shù)設(shè)置成-Xms64m -Xmx512m -XX:MaxPermSize=80m
          但是monitor 畫面中max heap為 256M
          點擊overview標簽,發(fā)現(xiàn)jvm參數(shù)如下:
          -Xms64m
          -Xmx512m
          -XX:MaxPermSize=80m
          -Xms64m
          -Xmx256m

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

          另外,使用jvisualvm持續(xù)監(jiān)測tomcat內(nèi)存情況發(fā)現(xiàn)heap區(qū)域的內(nèi)存使用會在一個高峰后穩(wěn)定降低,內(nèi)存被回收了;
          而PermGen區(qū)域的內(nèi)存則是不斷增加到達一個峰值后就不再增加,但之后此區(qū)的內(nèi)存沒有被回收,驗證了上面的說法;

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

          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.

          華麗的上層需要堅實的底層基礎(chǔ)

          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監(jiān)測設(shè)置合理值 2011-10-20 08:34 tb
          不錯 又收藏一個錯誤  回復  更多評論
            

          主站蜘蛛池模板: 开化县| 南江县| 虞城县| 东明县| 进贤县| 藁城市| 桑日县| 万山特区| 宁化县| 唐山市| 郧西县| 石门县| 年辖:市辖区| 平泉县| 扎赉特旗| 达日县| 彭山县| 扎囊县| 南江县| 监利县| 满洲里市| 达日县| 博野县| 互助| 皮山县| 龙门县| 松江区| 深水埗区| 益阳市| 石嘴山市| 九江市| 买车| 浦江县| 东乡族自治县| 阳春市| 东源县| 白水县| 博客| 陵水| 白沙| 延安市|