Turn: Java.lang.OutOfMemoryError:Java heap space error and handling (collect, turn)

Source: Internet
Author: User
Tags log log xms

The following is an error resolution for heap space overflow 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 in the JVM if 98% of the time is used for GC and 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 running the Java program.

The JVM automatically sets the value of the heap size when it starts, and its initial space (that is,-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 set it up.
Example: java-jar-xmn16m-xms64m-xmx128m Myapp.jar

If 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 more time and the application is assigned less execution time.

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 efforts to finally complete 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 directory 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 directory 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 this condition.
Then I add a-xmx256m to the parameters of the boot, and this is the time to go.

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 as a code to write the procedure:

(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 when the method call is complete, 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 enforce garbage collection-calling System.GC (), but this is also an indeterminate approach. Java does not guarantee that each call to this method will be able to start garbage collection, it will only issue to the JVM such a request, whether or not to actually perform garbage collection, everything is unknown.

(3) Pick a garbage collector that suits you. In general, if your system does not have special and demanding performance requirements, you can use the JVM's default options. Otherwise, you might consider using a targeted garbage collector, such as an incremental collector, which is more suitable for systems with higher real-time requirements. The system has a high configuration, there are more idle resources, you 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 programmers use temporary variables when the reference variable is automatically set to null after exiting the active domain (scope), implies that the garbage collector collects the object, and must be aware that the referenced object is listening, and if so, remove the listener and then assign a null value.

That is, it is better to take control of the frequent application of memory and free memory, but the System.GC () method does not necessarily apply, and it is best to use finallize to enforce or write your 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

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

If 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 in the JVM if 98% of the time is used for GC and the available HEAP size is less than 2%.
The JVM heap setting is the set of memory space that the JVM can provision during the run of the Java program. The JVM automatically sets the value of the heap size when it starts, and its initial space (that is,-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 set it up.
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 that sets the JVM size, because the Eclipse runtime requires the JVM itself, so the JVM size set in Eclipse.ini is not the size of the JVM used to run a particular program, regardless of the size of the JVM the program is running on.

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

Because 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 specific setup methods are:

Select the class that is running, click the menu ' run as->open run Dialog ... ', select the (x) =argument tab under the VM Arguments box, enter-xmx512m, save run 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 are min and Max memory, respectively
In Eclipse, the
Windows->preferences. ->TOMCAT->JVM. ->JVM that one text box, add-xms256m-xmx512m
That's it!

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

Right click, select Run ..., pop-up window for example, fill in the VM input box with the appropriate memory value.

==================================
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 this variable is not used.
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.

Specifies the memory of the Java virtual machine using parameters.
java-xms256m-xmx1024m-xx:maxpermsize=256m

========================================================
2. If a memory overflow problem occurs during startup, throw an exception message similar to the following:
Java.lang.OutOfMemoryError:Java Heap Space

There are ways to try it:
A. Modify Tomcat/bin/catalina.bat and add the following:
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, add parameter:-xms256m-xmx512m

Reference reason: This exception is thrown when the Heap size is less than 2% if 98% of the time in the JVM is used for GC and is available.
The JVM heap setting is the set of memory space that the JVM can provision during the run of the Java program. The JVM automatically sets the value of the heap size when it starts, and its initial space (that is,-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 set it up. 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.

Goto: Java.lang.OutOfMemoryError:Java heap Space error and handling (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.