Java.lang.OutOfMemoryError:Java Heap Space Error and handling method (collect, turn)

Source: Internet
Author: User
Tags log log xms

Here's how to troubleshoot heap overflow errors found on the Web:

Java.lang.OutOfMemoryError:Java Heap Space
===================================================

An exception occurred when using a Java program to query large amounts of data from a database:
Java.lang.OutOfMemoryError:Java Heap Space


This exception information is thrown when the JVM assumes that 98% of the time is used for GC and that the available Heap size is less than 2%.

The JVM heap setting is the set of memory space that the JVM can provision in the course of executing the Java program.

The JVM automatically sets the value of the heap size when it starts, with its initial space (that is,-XMS) being 1/64 of the physical memory, and the maximum space (-XMX) is 1/4 of the physical memory. Can be set using options such as-XMN-XMS-XMX provided by the JVM.
For example: java-jar-xmn16m-xms64m-xmx128m Myapp.jar

Assuming that the heap size setting is small, in addition to these exception information, you will notice that the program's responsiveness slows down. The GC takes up a lot of other time, and the application is assigned less time to run.

The Heap Size does not exceed 80% of the available physical memory, generally the-XMS and-XMX options are set to the same, and the-XMX value of-xmn is 1/4.
The-XMS-XMN setting of the Heap size does not exceed the size of the physical memory. Otherwise, "Error occurred during initialization of VM Could not reserve enough space for object heap" will be prompted.

==========================================================
After a night of hard work. A file to replace the specified string of programs, but because I want to replace the full-site program HTML file too many, so eclipse always in a folder after the end of the report Java.lang.OutOfMemoryError:Java heap Space's anomaly, and then it crashes.

I think it must be frequent operation caused by too late recovery, so in each cycle after adding a thread.sleep (1000), found that the folder is dead, so 1000 to 5000, or to die there, I think it may not be too late to recover so simple, maybe Sun JVM, it is also possible to not release such a situation.
Then I added a-xmx256m to the start-up, which I was able to do.

Think about it, or the principle of garbage collection is not very understanding, on-line check a bit, found a few good articles.

Http://java.ccidnet.com/art/3539/20060314/476073_1.html
Http://www.pconline.com.cn/pcedu/empolder/gj/java/0509/701281.html


Also: Java heap management-garbage collection mentioned a few points, very good, perhaps can be used as a code-knocking rules:

(1) Do not attempt to assume that garbage collection occurs at any time, all of which are unknown. For example, a temporary object in a method becomes a useless object after the method call is completed, and its memory can be freed at this time.

(2) Java provides a number of classes that deal with garbage collection and provides a way to forcibly run garbage collection-calling System.GC (), but this is an indeterminate approach. Java does not guarantee that each call to the method will be able to start garbage collection, it simply will be issued to the JVM such a request, whether or not actually run garbage collection, everything is an unknown.

(3) Pick a garbage collector that suits you. In general, assume that the system does not have special and demanding performance requirements and is able to adopt the JVM's default options. Otherwise, it is possible to consider using a targeted garbage collector, such as an incremental collector that is more suitable for systems with higher real-time requirements. The system has a high configuration, there are a lot of idle resources, can consider using the parallel tag/purge collector.

(4) The key is also difficult to grasp the problem is the memory leak. Good programming habits and rigorous programming attitude is always the most important, do not let your own a small error caused a large memory leak.

(5) Release the reference of the useless object as soon as possible.
Most program apes use temporary variables when they leave the reference variable to null after exiting the active domain (scope), implying that the garbage collector collects the object, and that the referenced object is listening, false, and then the listener is removed, and then the null value is assigned.

That is, for the frequent application of memory and release memory operations, or control the better, but the System.GC () method does not necessarily apply, it is best to use finallize forced to run or write their own finallize method.

================================================
Tomcat


Encounter Tomcat Error: Java.lang.OutOfMemoryError:Java heap space, then looked up the data, found the solution:
If Java runs out of memory, the following error occurs:
Exception in thread "main" Java.lang.OutOfMemoryError:Java heap space
Java heap size can be increased as follows:

Java-xms<initial heap size>-xmx<maximum Heap size>
Defaults is:
java-xms32m-xmx128m

Suppose you use win
/tomcat/bin/catalina.bat Add the following command:
Set java_opts=-xms32m-xmx256m

Let's say you use Unix/linux.
/tomcat/bin/catalina.sh Add the following command:
Java_opts= "-xms32m-xmx256m"


=========================================
Reason:


An exception occurred when using a Java program to query large amounts of data from a database:
Java.lang.OutOfMemoryError:Java Heap Space
This exception information is thrown when the JVM assumes that 98% of the time is used for GC and that the available Heap size is less than 2%.
The JVM heap setting is the set of memory space that the JVM is able to provision during the execution of the Java program. The JVM automatically sets the value of the heap size on its own when it is started, and its initial space (that is,-XMS) is 1/64 of physical memory, and the maximum space (-XMX) is 1/4 of physical memory. Can be set using options such as-XMN-XMS-XMX provided by the JVM.
The workaround:
Tomcat_home/bin Catalina.bat (Win) or catalina.sh (Linux) before executing the code plus:
Set java_opts=%java_opts%-xms128m-xmx512m

=============================================================
Eclipse Java.lang.OutOfMemoryError:Java Heap Space Solution
===========================================================
Eclipse Java.lang.OutOfMemoryError:Java Heap Space Solution

Eclipse has a boot parameter to set the JVM size, since Eclipse executes its own JVM, so the JVM size set in Eclipse.ini is not the size of the JVM used to detail a program's execution, regardless of the size of the JVM that the detail program executes.

So how to set the JVM size of a program (of course, the console does not have this problem, such as: java-xms256m-xmx1024m classname, so that the current program's JVM size can be set)?

Since the JVM configuration of one of the default programs in Eclipse is:-xms8m-xmx128m, we need to manually adjust the amount of memory to be processed so that there is no memory overflow. The detailed setup method is:

Select the class to be executed, click the menu ' run as->open run Dialog ... ', select the (x) =argument tab under the VM Arguments box to enter-xmx512m, save execution OK

=======================================================
Java.lang.OutOfMemoryError:Java Heap space in eclipse

Error 2007-06-12 11:53java.lang.outofmemoryerror:java heap space error

In the Tomcat in Catalina.bat Riga
Set Java_opts=-xms256m-xmx512m-djava.awt.headless=true
256 and 512 each are min and Max memory
In Eclipse, the
Windows->preferences. ->TOMCAT->JVM. ->JVM that text box, add-xms256m-xmx512m
This will do it!

==================================
VM setting method in Eclipse-[]-Tag:java.lang.OutOfMemoryError:Java heap space

Right-click, choose Run ..., pop-up the form for example, fill in the VM input box with the appropriate memory value can be.

==================================
In eclipse, the Yellow line is a warning.
Private log log = Logfactory.getlog (addcateaction.class);//small Yellow line on Log.
The yellow line here means that you have defined the private variable log, but it is not practical to this variable.
V.add (o); A small yellow line appears below eclipse to indicate that it is still being referenced?
This refers to the method that the Add method is not recommended.

OutOfMemoryError is not related to a suspected reference, and may be a loop-created object that causes insufficient Java memory.

Specify the memory of the Java Virtual machine using the number of parameters.
java-xms256m-xmx1024m-xx:maxpermsize=256m

========================================================
2. Assume that a memory overflow problem occurs during startup, throwing similar exceptions such as the following:
Java.lang.OutOfMemoryError:Java Heap Space

Can try to:
A. Change the Tomcat/bin/catalina.bat, add for example the following content
Set java_opts=-xms256m-xmx512m-djava.awt.headless=true [-xx:maxpermsize=128m]

B.eclipse->windows->preferences. ->TOMCAT->JVM. ->JVM text box, add-xms256m-xmx512m

C.eclipse->preference->java->instal Jres->edit, added:-xms256m-xmx512m

Reason: The JVM assumes that 98% of the time is used for GC and is available, this exception is thrown when the Heap size is less than 2%.
The JVM heap setting is the set of memory space that the JVM is able to provision during the execution of the Java program. The JVM automatically sets the value of the heap size on its own when it is started, and its initial space (that is,-XMS) is 1/64 of physical memory, and the maximum space (-XMX) is 1/4 of physical memory.
Can be set using options such as-XMN-XMS-XMX provided by the JVM. The Heap Size does not exceed 80% of the available physical memory, generally the-XMS and-XMX options are set to the same, and the-XMX value of-xmn is 1/4.
The-XMS-XMN setting of the Heap size does not exceed the size of the physical memory. Otherwise, "Error occurred during initialization of VM Could not reserve enough space for object heap" will be prompted.

==================================================================

Reasons for my problem:

Using Spring+hibernate to read large amounts of data from a database, using a singleton pattern of Appcontext Getbean, a large number of entity beans are cached in memory ...

There is still no effective way to mitigate this by increasing the size of the JVM's memory and altering the way data is read (such as reading only practical information).

Change the way data is read:
For example a Entitybean has the following fields: ID, name, age, address, description. Assuming that the program only uses this bean's ID and Name field, then only read the two fields from the database, rather than reading the entire bean and the associated bean.

Java.lang.OutOfMemoryError:Java Heap Space Error and handling method (collect, turn)

Related Article

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.