How to deal with “java.lang.OutOfMemoryError: Java heap space” error?

后端 未结 21 2122
死守一世寂寞
死守一世寂寞 2020-11-21 04:49

I am writing a client-side Swing application (graphical font designer) on Java 5. Recently, I am running into java.lang.OutOfMemoryEr

21条回答
  •  南旧
    南旧 (楼主)
    2020-11-21 05:33

    By default for development JVM uses small size and small config for other performance related features. But for production you can tune e.g. (In addition it Application Server specific config can exist) -> (If there still isn't enough memory to satisfy the request and the heap has already reached the maximum size, an OutOfMemoryError will occur)

    -Xms        set initial Java heap size
    -Xmx        set maximum Java heap size
    -Xss        set java thread stack size
    
    -XX:ParallelGCThreads=8
    -XX:+CMSClassUnloadingEnabled
    -XX:InitiatingHeapOccupancyPercent=70
    -XX:+UnlockDiagnosticVMOptions
    -XX:+UseConcMarkSweepGC
    -Xms512m
    -Xmx8192m
    -XX:MaxPermSize=256m (in java 8 optional)
    

    For example: On linux Platform for production mode preferable settings.

    After downloading and configuring server with this way http://www.ehowstuff.com/how-to-install-and-setup-apache-tomcat-8-on-centos-7-1-rhel-7/

    1.create setenv.sh file on folder /opt/tomcat/bin/

       touch /opt/tomcat/bin/setenv.sh
    

    2.Open and write this params for setting preferable mode.

    nano  /opt/tomcat/bin/setenv.sh 
    
    export CATALINA_OPTS="$CATALINA_OPTS -XX:ParallelGCThreads=8"
    export CATALINA_OPTS="$CATALINA_OPTS -XX:+CMSClassUnloadingEnabled"
    export CATALINA_OPTS="$CATALINA_OPTS -XX:InitiatingHeapOccupancyPercent=70"
    export CATALINA_OPTS="$CATALINA_OPTS -XX:+UnlockDiagnosticVMOptions"
    export CATALINA_OPTS="$CATALINA_OPTS -XX:+UseConcMarkSweepGC"
    export CATALINA_OPTS="$CATALINA_OPTS -Xms512m"
    export CATALINA_OPTS="$CATALINA_OPTS -Xmx8192m"
    export CATALINA_OPTS="$CATALINA_OPTS -XX:MaxMetaspaceSize=256M"
    

    3.service tomcat restart

    Note that the JVM uses more memory than just the heap. For example Java methods, thread stacks and native handles are allocated in memory separate from the heap, as well as JVM internal data structures.

提交回复
热议问题