Tomcat - java.lang.OutOfMemoryError:PermGenスペース

Tomcat – java.lang.OutOfMemoryError:PermGenスペース

多くの場合、Tomcatは次のjava.lang.OutOfMemoryError: PermGen spaceエラーに遭遇する可能性があります。

java.lang.OutOfMemoryError: PermGen space
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)

通常、Tomcatが数回再起動した後に発生します。

1. 溶液

デフォルトでは、Tomcatには実行中のプロセス用にごくわずかなPermGenメモリが割り当てられます。 修正するには、次のJava VMオプションを使用してPermGenメモリ設定を増やします。

-XX:PermSize - Set initial PermGen Size.
-XX:MaxPermSize - Set the maximum PermGen Size.

次のステップでは、WindowsおよびLinux環境でTomcatにVMオプションを設定する方法を示します。

2. Windows

Tomcatはこのスクリプトファイルcatalina.batによって管理されており、スクリプト内に飛び込むと、catalina.batが常にsetenv.batファイルを見つけて実行し、環境変数を設定していることがわかります。

\{$tomcat-folder}in\catalina.bat

//...
rem Get standard environment variables
if not exist "%CATALINA_BASE%\bin\setenv.bat" goto checkSetenvHome
call "%CATALINA_BASE%\bin\setenv.bat"
goto setenvDone
:checkSetenvHome
if exist "%CATALINA_HOME%\bin\setenv.bat" call "%CATALINA_HOME%\bin\setenv.bat"
:setenvDone
//...

2.1 To set the environment variable on Windows, create a setenv.bat manually, and put it into the ${tomcat-folder}in folder.

${tomcat-folder}in\setenv.bat

set JAVA_OPTS=-Dfile.encoding=UTF-8 -Xms128m -Xmx1024m -XX:PermSize=64m -XX:MaxPermSize=256m

P.S No double quotes, set JAVA_OPTS={value}.

2.2 Restart Tomcat, it will call the setenv.bat file to set the environment variable automatically.

{$tomcat-folder}\bin\catalina.bat restart

3. Linux

Linuxでもプロセスは同じで、Tomcatが代わりにcatalina.shsetenv.shを使用しています。

3.1 Find out where is catalina.sh :

catalina.sh

$ sudo find / -name "catalina.sh"
Password:
find: /dev/fd/3: Not a directory
find: /dev/fd/4: Not a directory
/Users/example/Downloads/apache-tomcat-6.0.35/bin/catalina.sh

3.2 Review the catalina.sh, script, it behaves like Windows, but use setenv.sh instead.

//...
# Ensure that any user defined CLASSPATH variables are not used on startup,
# but allow them to be specified in setenv.sh, in rare case when it is needed.
CLASSPATH=

if [ -r "$CATALINA_BASE/bin/setenv.sh" ]; then
  . "$CATALINA_BASE/bin/setenv.sh"
elif [ -r "$CATALINA_HOME/bin/setenv.sh" ]; then
  . "$CATALINA_HOME/bin/setenv.sh"
fi
//...

3.3 Create a setenv.sh manually, and put it into the ${tomcat-folder}in\ folder.

${tomcat-folder}in\setenv.sh

export JAVA_OPTS="-Dfile.encoding=UTF-8 -Xms128m -Xmx1024m -XX:PermSize=64m -XX:MaxPermSize=256m"

P.S With double quotes, export JAVA_OPTS=”{value}”.

3.4 Restart Tomcat.

Note
ヒープサイズと非ヒープサイズ(perm gen)の値は単なる例であり、プロジェクトのニーズに合わせて値を変更する必要があります。