Exception in thread & quot; main & quot; java. lang. OutOfMemoryError: Java heap space, exceptioninthread

Source: Internet
Author: User

Exception in thread "main" java. lang. OutOfMemoryError: Java heap space, exceptioninthread
Exception in thread "main" java. lang. OutOfMemoryError: Java heap space Solution

Problem descriptionException in thread "main"Java. lang. OutOfMemoryError: Java heap space

Solution [go]It has always been known that jvm heap size can be set, and java programs are always written/debugged using eclipse. Run the program by adding parameters on the command line or console. Symptom: set it in the eclipse configuration file eclipse. ini.-Vmargs-Xms500m-Xmx1024mJava. lang. OutOfMemoryError: Java Heap Space error still occurs when you run or debug some memory-consuming programs in eclipse, which is generally considered to be insufficient memory and insufficient memory of the java Virtual Machine. Adding these parameters in the command line will be effective and will not cause errors. This indicates a problem where these parameters do not work at all. You need to debug the program in eclipse today. heap error occurs before debugging. You searched many places on the Internet and got the final answer: select the class to be run and click the menu'Run-> run...', SelectVm argumentsEnter-Xmx800m, Save and run. OriginallyYou need to set each project separately, Khan...

 


There are three possible causes of OutOfMemoryError. First, the JVM has a real memory leak, which causes a Bug in the internal implementation of the JVM heap. This is extremely unreliable. All JVMs have been fully tested, and if someone finds this bug, it will definitely be the highest priority. Therefore, you can eliminate this possibility with great relief.
The second possible reason for OutOfMemoryError is that you did not provide enough available memory for your application to run. In this case, there are two possible solutions, or increase the JVM heap available size, or reduce the total amount of memory required by your application. To increase the JVM available heap size, you can simply use the JVM-Xmx parameter. If you set this parameter as large as possible (the maximum available memory should not exceed the physical memory of the system, otherwise your application will be paged and paused), there will still be the memory problems mentioned above, then, you need to reduce the amount of memory that your application may use. Reducing the application memory may be simple. You may allow some collections to be too large, such as using many large buffers. Or it is too complicated, requiring you to implement some classes and even redesign the application.
Reader Jams Stauffer points out that some JVMs (such as sun's JVMs) also have a "Perm" parameter to process JVM structures and class objects. If you are using a very large number of class sets, it may run outside the "Perm" space, and then you need to increase the size of the space, for example, sun's JVM uses the-XX: PermSize and-XX: MaxPermSize options.


Third, OutOfMemoryError is the most common and unintentional object reference persistence. You do not release objects clearly, so that your heap grows until you have no additional space.
Processing OutOfMemoryError:
Is it a JVM internal BUG? Unlikely. If so, this is the highest priority BUG (Why have no one found it, but you have encountered it ?).
Is there enough memory allocated to the actual running application? Two options: Use the-Xmx parameter to increase the maximum memory usage of the heap (or use the-XX: MaxPermSize parameter to increase the size of the Perm space ); or use a smaller set/buffer/tablespace/object ....., in order to reduce the total amount of memory required, that is, you can adjust the object size, redesign and re-implement your application process.

Unintentional object reference persistence? Find the source object that has not been referenced, change it, and release the object. The article outline in the IBM developer community reveals such a general process. This process mainly waits until the application reaches a constant state-you will expect the most newly created objects to be temporary objects and can be collected by the garbage collector. This is often after all the initialization work of the application is completed.
Forced garbage collection to get a heap object snapshot. Doing any work may be importing to unintentional object reference persistence. Forces another garbage collection and obtains the object snapshot of the second heap. Compare the two snapshots to see which objects have increased in quantity from the first snapshot to the second snapshot. Because you force the garbage collection before the snapshot, the rest will be all objects referenced by the application, comparing two snapshots accurately identifies the newly created objects that are retained in the application. Based on your understanding of the application, you can determine which objects are unintentionally retaining object references in two snapshot comparisons. Track the leading references and find the objects that are referencing these unintentional holding objects until you find the source object that causes this problem.
When starting a virtual machine, add a parameter-Xms800m-Xmx800m. -Xms <size> sets the JVM initialization heap memory size.
-Xmx <size> sets the maximum JVM heap memory size.
If it is an application, add this parameter to the Startup File of your class name, such as tomcat, for java-Xms800m-Xmx800m.


In addition, set the environment variable JAVA_OPTS = "-server-Xms800m-Xmx800m-XX: PermSize = 64 M-XX: MaxNewSize = 256 m-XX: MaxPermSize = 128 m-Djava. awt. headless = true"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.