Exception in thread "main" Java.lang.OutOfMemoryError:Java Heap space (Java Heap Spatial memory overflow) workaround

Source: Internet
Author: User
Tags xms

Http://hi.baidu.com/619195553dream/blog/item/be9f12adc1b5a3e71f17a2e9.html Problem Description
Exception in thread "main" Java.lang.OutOfMemoryError:Java heap Space

solution [Go]
Always know you can set the JVM heap size and always write/debug Java programs with Eclipse. Always use the command line or console plus parameters to run the program. Symptom: Set in Eclipse configuration file Eclipse.ini -vmargs-xms500m-xmx1024m, the Java.lang.OutOfMemoryError:Java Heap space error is still present in eclipse when running directly or debugging some memory-consuming programs, which is generally considered insufficient memory and Java virtual machine memory is not sufficient. Adding these parameters to the command line is effective and does not make an error. This illustrates a problem where these parameters do not work at all. Today needs to debug the program in eclipse, not to need to debug where the heap error, search the Internet a lot of places, got the final answer:
Select the class to be run, click the menu ' Run->run ...', select (x) the =argument tab under the vm ArgumentsIn the box
Input -xmx800m, save the run.
Originally also each project needs to be individually setSweat...


There are three possible causes of OutOfMemoryError. First, this JVM has a real memory leak, causing a bug in the internal implementation of this JVM heap. This is extremely unreliable. All JVMs are fully tested and, if anyone finds such a bug, it will definitely be the highest priority. So you can eliminate this possibility very carefully.

The second possible reason for OutOfMemoryError is simply that you are not giving enough memory for your application to run. In this case, there are two possible scenarios, either increasing the size of the JVM heap available, or reducing the amount of memory required by your application. The JVM's-xmx parameters can be easily used to increase the available heap size of the JVM. If you set this parameter as large as possible (the free memory limit does not exceed the system physical memory, your application will be paged and paused), and still have the memory problems mentioned above, then you need to reduce the amount of memory that your application may use. Reducing application memory may be simple, and you may allow some collections to be too large, such as using many large buffers. Or it's overly complex, requiring you to re-implement some classes and even redesign your application.

Reader Jams Stauffer points out that some jvms, such as Sun's JVMs, also have a "Perm" parameter to handle JVM structures and class objects. If you are using a very large set of classes, it is possible to run outside of the "Perm" space, and then you need to increase the size of this space, for example, the Sun's JVM uses the-xx:permsize and-xx:maxpermsize options.

The third cause of outofmemoryerror is the most common, unintentional object reference to keep. You don't have an unmistakable release object so that your heap grows again, until you have no extra space.

Handling OutOfMemoryError:

Is it a bug inside the JVM? It's not likely. If so, this is the highest priority bug (why hasn't anyone found it yet, and you ran into it?) )。

Not enough memory allocated to the actual running application? Two options: Use the-XMX parameter to increase the maximum memory usage of the heap (or increase the perm space size using the-xx:maxpermsize parameter); Or use a smaller collection/buffer/tablespace/object ... to reduce the amount of memory needed, that is, you can resize the object, redesign and re-implement your application.

Unintentional object reference hold? Locate the source object that holds these unintentional references, change it, and release the objects. The article in the IBM developer community outlines such a common process. The process is to wait until the application arrives at a constant state-the most newly created object you will expect is a temporary object and can be collected by the garbage collector. This is often done after all initialization work for the application is complete.

Force garbage collection to get a heap of object snapshots.
Doing any work may be leading to unintentional object reference retention.
Force another garbage collection and get an object snapshot of the second heap.
Compare these two snapshots to see which objects are increasing in number from the first snapshot to the second snapshot. Because you force garbage collection before the snapshot, the remainder will be all objects referenced by the application, and a comparison of two snapshots will accurately identify the newly created objects that remain in the application.
Based on your understanding of the application, determine which objects are inadvertently holding object references in two snapshot comparisons.
Trace leading references to find which objects are referencing these unintentional persisted objects until you find the source object that is causing the problem

When you start the virtual machine, add a parameter:-xms800m-xmx800m is good.
-xms <size>
Set JVM initialization heap memory size

-xmx <size>
To set the maximum heap memory size for the JVM

If it is an application, then: java-xms800m-xmx800m Your class name
If it is a Web server such as tomcat, add this parameter to the server's startup file.


Also set environment variables
Java_opts= "-server-xms800m-xmx800m-xx:permsize=64m-xx:maxnewsize=256m-xx:maxpermsize=128m-djava.awt.headless= True "Java application Configuration program JVM memory size,-xms minimum,-xmx maximum,-xmx cannot be less than-XMS

Exception in thread "main" Java.lang.OutOfMemoryError:Java Heap space (Java Heap Spatial memory overflow) workaround

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.