天天看點

PermGen space

PermGen space的全稱是Permanent Generation space,是指記憶體的永久儲存區域OutOfMemoryError: PermGen space從表面上看就是記憶體益出,解決方法也一定是加大記憶體。說說為什麼會記憶體益出:這一部分用于存放Class和Meta的資訊,Class在被 Load的時候被放入PermGen space區域,它和和存放Instance的Heap區域不同,GC(Garbage Collection)不會在主程式運作期對PermGen space進行清理,是以如果你的APP會LOAD很多CLASS的話,就很可能出現PermGen space錯誤。這種錯誤常見在web伺服器對JSP進行pre compile的時候。 如果你的WEB APP下都用了大量的第三方jar,其大小 超過了jvm預設的大小(4M)那麼就會産生此錯誤資訊了。 

解決方法: 手動設定MaxPermSize大小 

改正方法:-Xms256m -Xmx256m -XX:MaxNewSize=256m -XX:MaxPermSize=256m

修改TOMCAT_HOME/bin/catalina.sh 

JAVA_OPTS="-server -XX:PermSize=64M -XX:MaxPermSize=128m 

建議:将相同的第三方jar檔案移置到tomcat/shared/lib目錄下,這樣可以達到減少jar 文檔重複占用記憶體的目的。 

Sun文檔是這樣解釋的:

java.lang.OutOfMemoryError: PermGen space

The detail message PermGen space indicates that the <b>permanent generation</b> is full. The 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.

下面是某人遇到的問題:

SUN JDK+Tomcat 5.5.20運作服務的時候遇到問題,伺服器跑幾天後就會挂掉,并報java.lang.OutOfMemoryError: PermGen space異常。

但問題是為什麼這些王牌的開源會出現同一個問題呢,那麼是不是更基礎的原因呢?tomcat在Q&amp;A很隐晦的回答了這一點。(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 causeOufOfMemoryErrors eventually. )

在tomcat中redeploy時出現outofmemory的錯誤. 

可以有以下幾個方面的原因: 

1,使用了proxool,因為proxool内部包含了一個老版本的cglib. 

2, log4j,最好不用,隻用common-logging 

3, 老版本的cglib,快點更新到最新版。 

4,更新到最新的hibernate3.2 

3、 這裡以tomcat環境為例,其它WEB伺服器如jboss,weblogic等是同一個道理。

二、java.lang.OutOfMemoryError: Java heap space 

Heap size 設定 

JVM堆的設定是指java程式運作過程中JVM可以調配使用的記憶體空間的設定.JVM在啟動的時候會自動設定Heap size的值, 

其初始空間(即-Xms)是實體記憶體的1/64,最大空間(-Xmx)是實體記憶體的1/4。可以利用JVM提供的-Xmn -Xms -Xmx等選項可 

進行設定。Heap size 的大小是Young Generation 和Tenured Generaion 之和。 

提示:在JVM中如果98%的時間是用于GC且可用的Heap size 不足2%的時候将抛出此異常資訊。 

提示:Heap Size 最大不要超過可用實體記憶體的80%,一般的要将-Xms和-Xmx選項設定為相同,而-Xmn為1/4的-Xmx值。 

解決方法:手動設定Heap size 

在“echo "Using CATALINA_BASE:   $CATALINA_BASE"”上面加入以下行: 

JAVA_OPTS="-server -Xms800m -Xmx800m   -XX:MaxNewSize=256m" 

三、執行個體,以下給出1G記憶體環境下java jvm 的參數設定參考: 

JAVA_OPTS="-server -Xms800m -Xmx800m  -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128m -Djava.awt.headless=true " 

針對Tomcat,如果Tomcat下面有多個應用,盡可能的把lib下共用的jar檔案放到Tomcat的lib下,釋出速度和運作速度上也有所提升。

題外話:經常看到網友抱怨tomcat的性能不如...,不穩定等,其實根據筆者幾年的經驗,從"互聯星空“到現在的房産門戶網,我們 均使用tomcat作為WEB伺服器,每天通路量百萬多,tomcat仍然運作良好。建議大家有問題多從自己程式入手,多看看java的DOC文檔。

=======================================================

【解決辦法】就是設定JVM啟動的時候參數,可以如下設定:

java -XX: PermSize=64m -XX: MaxPermSize=128m

另外PermSize 和MaxPermSize如果設定為相同還可以在一定程度上提高性能,因為,PermSize在不斷的變化中會需要轉移其中的資料。如果固定了以後,則可以減少每次擴大PermSize帶來的性能損失。

另外,還可以在Java啟動的時候添加下面的參數來看GC的運作情況:

Java -verbosegc 本文轉自chainli 51CTO部落格,原文連結:http://blog.51cto.com/lichen/192681,如需轉載請自行聯系原作者