Java.lang.OutOfMemoryError:Java Heap Space Memory problem __java

Source: Internet
Author: User
Tags garbage collection memory usage xms

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

Problem description
Exception in thread "main" Java.lang.OutOfMemoryError:Java heap

solution [Turn]
Always know that you can set the JVM heap size and always write/debug Java programs with Eclipse. Always run the program with the command line or console plus parameters. Behavior: Set-vmargs-xms500m-xmx1024m in Eclipse's configuration file Eclipse.ini, run directly in eclipse Or debug some memory-consuming programs still appear Java.lang.OutOfMemoryError:Java Heap space error, that is, generally considered insufficient memory, Java virtual machine memory is not enough. These parameters are effective at the command line, and there is no error. This illustrates a problem where these parameters do not work at all. Today, you need to debug in eclipse, not to need to debug the place on the heap error, in the online search a lot of places, got the final answer:
Select the class that is being run, click on the menu ' Run->run ... ' and select (x) =argument The VM arguments box under the tab page
Enter-xmx800m and save the run.
It turns out that you need to set up each project individually, sweat ...


There are three possible causes of OutOfMemoryError.

First, this JVM has a real memory leak, causing the JVM heap to generate a bug when implemented internally. This is extremely unreliable. All JVMs are fully tested and, if someone discovers this bug, it is definitely the highest priority. So you can rule out this possibility very carefully.

The second possible outofmemoryerror reason is simply that you don't have enough memory available to run your application. In this case, there are two possible scenarios, either increasing the size of the JVM heap available, or reducing the amount of memory your application needs. Increasing the JVM's available heap size can simply use the JVM's-xmx parameters. If you set this parameter as large as possible (the available memory limit does not exceed the system physical memory, or your application will be paged and paused), and still have the memory problem mentioned above, you need to reduce the amount of memory 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 too complex to require you to implement some classes, or even redesign your application.

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

The third causes OutOfMemoryError to be the most common, unintentional object reference retention. You are not releasing objects explicitly so that your heap grows again until you have no extra space.

Processing OutOfMemoryError:

is a bug inside the JVM. It's not possible. If so, this is the highest priority bug (why no one has found it, and you've met it.) )。

There is not enough memory to allocate 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 set/buffer/table space/object ... to reduce the total amount of memory needed, that is, you can resize the object, redesign and re-implement your application.

Unintentional object references are persisted. Find the source object that holds these unintentional references, change it, and release the objects. An article in the IBM Developer Community outlines a common process. This process is primarily to wait 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 done after all of the initialization work for the application is complete.

Force garbage collection to get a snapshot of the object of a heap.
Doing any work may be leading to unintentional object reference retention.
Force another garbage collection and get the object snapshot for the second heap.
Compare the two snapshots to see which objects have increased in number from the first snapshot to the second snapshot. Because you force garbage collection before the snapshot, the rest will be the objects referenced by the application, and comparing two snapshots will accurately identify those newly created objects that remain in the application.
Depending on your knowledge of the application, decide which objects in the two snapshot comparisons are inadvertently holding the object reference.
Track leading references to find which objects are referencing these unintentional persisted objects until you find the source object that caused the problem

When you start the virtual machine, add a parameter:-xms800m-xmx800m is fine.
-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's a Web server like tomcat, add this parameter after the server's startup file.


Set up environment variables in addition
Java_opts= "-server-xms800m-xmx800m-xx:permsize=64m-xx:maxnewsize=256m-xx:maxpermsize=128m-djava.awt.headless= True "


Turn 2

First check that the program has no limit to the dead loop

This problem is mainly caused by the problem Java.lang.OutOfMemoryError:Java heap space. The first time such a problem arises, other problems arise. A search on the web may be the reason why the Java stack setting is too small.
According to the answers on the internet, there are roughly two ways to solve this problem:
1. Set environment variable

Workaround: Manually set heap Size
Modify Tomcat_home/bin/catalina.sh
Set java_opts=-xms32m-xmx512m
You can make changes based on your machine's memory.

2, java-xms32m-xmx800m ClassName
is to add this parameter when executing the Java class file, where ClassName is required to perform a true class name. (including package name)
This solves the problem. And the speed of execution is much faster than when it is not set.

If you might use Eclispe at the time of the test, you would need to enter-xms32m-xmx800m This parameter in the VM arguments in Eclipse->run-arguments.

Later, the boot parameters were modified in Eclilpse, and-xms32m-xmx800m was added to the VM arguments to solve the problem.

First, Java.lang.OutOfMemoryError:PermGen space

PermGen space is the full name of the permanent Generation spaces, refers to the memory of the permanent storage area,
This memory is primarily stored in class and meta information by the JVM, and class is placed in PermGen space when it is loader.
Unlike the heap region where the class instance (Instance) is stored, the GC (garbage Collection) does not
PermGen space for cleaning, so if your application has a lot of class, it is likely to appear permgen space error,
This error is common when the Web server is pre compile the JSP. If your web app uses a lot of third-party jars, its size
This error message is generated if the JVM default size (4M) is exceeded.
Workaround: Manually set the MaxPermSize size

Modify Tomcat_home/bin/catalina.sh
In the "echo" Using catalina_base: $CATALINA _base "", add the following line:
Java_opts= "-server-xx:permsize=64m-xx:maxpermsize=128m
Recommendation: Move the same third-party jar files to the Tomcat/shared/lib directory, which will reduce the memory consumption of the jar document.

Second, Java.lang.OutOfMemoryError:Java heap space
Heap Size Setting
The JVM heap setting is the setting of the memory space that the JVM can allocate to the Java program while it is running. The JVM automatically sets the value of heap size at startup.
Its initial space (i.e.-XMS) is 1/64 of the physical memory, and the maximum space (-XMX) is 1/4 of the physical memory. You can use options such as the-XMN-XMS-XMX provided by the JVM to
is set. Heap size is the sum of young Generation and tenured generaion.
Tip: This exception message is thrown in the JVM if 98% of the time is for GC and the available heap size is less than 2%.
Tip: Heap Size is not greater than 80% of the available physical memory, typically to set the-XMS and-XMX options to the same, and-xmn to the-XMX value of 1/4.
Workaround: Manually set heap Size
Modify Tomcat_home/bin/catalina.sh
In the "echo" Using catalina_base: $CATALINA _base "", add the following line:
Java_opts= "-server-xms800m-xmx800m-xx:maxnewsize=256m"

Iii. examples, the following gives a reference to the parameter settings of the Java JVM in the 1G memory environment:

Java_opts= "-server-xms800m-xmx800m-xx:permsize=64m-xx:maxnewsize=256m-xx:maxpermsize=128m-djava.awt.headless= True "


Large Web engineering, the memory space allocated by Tomcat default cannot be started, and if Tomcat is not started in MyEclipse it can be set for Tomcat:


Tomcat_home/bin/catalina.bat Add this sentence:

Set java_opts=-xmx1024m-xms512m-xx:maxpermsize=256m

If you want to start in MyEclipse, the above changes will not work, you can set the following:

In the MYECLIPSE->PREFERENCES->MYECLIPSE->SERVERS->TOMCAT->TOMCATX.X->JDK panel

Optional Java VM arguments add:-xmx1024m-xms512m-xx:maxpermsize=256m

Optional Java VM arguments add:-xmx1024m-xms512m-xx:maxpermsize=256m

It's very spiritual.

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.