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

后端 未结 21 1680
死守一世寂寞
死守一世寂寞 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:26

    Easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options -Xmx512M, this will immediately solve your OutOfMemoryError. This is my preferred solution when I get OutOfMemoryError in Eclipse, Maven or ANT while building project because based upon size of project you can easily ran out of Memory.

    Here is an example of increasing maximum heap size of JVM, Also its better to keep -Xmx to -Xms ration either 1:1 or 1:1.5 if you are setting heap size in your java application.

    export JVM_ARGS="-Xms1024m -Xmx1024m"

    Reference Link

    0 讨论(0)
  • 2020-11-21 05:32

    Increasing the heap size is not a "fix" it is a "plaster", 100% temporary. It will crash again in somewhere else. To avoid these issues, write high performance code.

    1. Use local variables wherever possible.
    2. Make sure you select the correct object (EX: Selection between String, StringBuffer and StringBuilder)
    3. Use a good code system for your program(EX: Using static variables VS non static variables)
    4. Other stuff which could work on your code.
    5. Try to move with multy THREADING
    0 讨论(0)
  • 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<size>        set initial Java heap size
    -Xmx<size>        set maximum Java heap size
    -Xss<size>        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.

    0 讨论(0)
提交回复
热议问题