search

Home  >  Q&A  >  body text

作为java项目OutOfMemoryError怎么处理,不是javaweb,不是tomcat溢出?

不是web项目,纯java项目
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

PHPzPHPz2893 days ago433

reply all(4)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:37:28

    I don’t agree with @Founder’s opinion, and I have seen many people start to change the size of the heap memory as soon as OOM occurs, and adjusting the heap memory may not necessarily mean bigger is better.

    First, spread the knowledge:

    • The heap memory is divided into new generation and old generation. The new generation is used to store objects that often need to be GC. Each object instance will have a count. If it has not been GCed for more than a certain period of time, it will Promoted to the old generation for storage.

    • The old generation will have larger space than the new generation.

    • When OOM occurs, there is insufficient memory in the old generation (because when the new generation is insufficient, it will be placed in the old generation for storage).

    It is recommended to analyze and solve the problem in the following way:

    1. Check the code directly to see if there are any memory leaks such as infinite loops or unreleased element references in the collection.

    2.Set JVM parameters -XX:+HeapDumpOutOfMemoryError-XX:HeapDumpPath=<你的path>

    3. Use the memory image analysis tool (http://www.eclipse.org/mat/) to analyze whether it is memory leak or memory overflow. In the case of memory leak, how to find the leaked object and GC ROOTS associated, and then find the location of the leaked code.

    4. If there is no memory leak, combined with the physical memory of the machine, see if you can continue to increase the heap memory (-Xms and -Xmx). Also note here that the bigger the heap memory setting, the better, because it is very easy. When FULL GC occurs in large memory, "stop the world" will be sent for a long time, which is unacceptable

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:37:28

    The simple solution is to adjust the heap size
    Modify the startup parameters in Eclilpse and add -Xms384m -Xmx384m to VM arguments
    or
    Add -Xms384m -Xmx384m to tomcat's catalina.bat

    However, it is recommended to check the code again

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:37:28

    @letcheng agrees with his point of view. You are just a pure Java project, not a web project. How many objects can you create and cause OOM on the heap? Therefore, it is largely due to your code.
    Under JDK, there is the built-in JConsole.exe tool. You can observe how many objects you currently have, how much heap memory there is, and when did OOM occur?

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:37:28

    For this kind of problem, it is recommended to use tools such as findbug or sonar to run the program to see where there is a risk of memory leakage leading to memory overflow

    java.lang.OutOfMemoryError: Java heap space is the young and old state of the heap memory. It will be thrown when the memory space is used up. So generally speaking, this problem is mainly caused by the created memory that cannot be recycled. For example, the most common IO class is used up. Do not do xx.close() afterwards

    Just configuring a large memory in TOMCAT is not enough, you need to find the source. If there is a memory leak problem, no matter how large the memory is, it will be useless

    reply
    0
  • Cancelreply