) Java. Lang. outofmemoryerror: Java heap space error and troubleshooting methods (Collection and conversion)

Source: Internet
Author: User
Tags xms

Java. Lang. outofmemoryerror: Java heap Space
========================================================== ==============

Use JavaProgramAn exception occurred when querying a large amount of data from the database:
Java. Lang. outofmemoryerror: Java heap Space

This exception is thrown if 98% is used for GC and the available heap size is less than 2% in JVM.

JVM heap setting refers to the setting of memory space that JVM can allocate during Java program running.

The JVM automatically sets the heap size value at startup. The initial space (-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-xmn-XMS-xmx provided by JVM to set the configuration.
Example: Java-jar-xmn16m-xms64m-xmx128m MyApp. Jar

If the heap size is set too small, in addition to the exception information, the response speed of the program will also be slow. GC takes more time, and the execution time assigned by the application is less.

The heap size should not exceed 80% of the available physical memory. Generally, the-XMS and-xmx options must be set to the same, and-xmn is the-xmx value of 1/4.
The-XMS-xmn setting of heap size should not exceed the physical memory size. Otherwise, the system prompts "error occurred during initialization of VM cocould not reserve enough space for object Heap ".

========================================================== ============================
After one night of hard work, I finally completed a program for replacing the specified string with a file. However, because there are too many HTML files for the whole site program I want to replace, I always report the Java code after a directory ends in eclipse. lang. outofmemoryerror: Java heap space exception, and then crash.

I thought it was definitely because of frequent operations that could not be recycled, so I added a thread after each loop. sleep (1000), I found that it still died in that directory, so I changed 1000 to 5000, or went there to die, I think it may not be too easy to recycle, maybe Sun's JVM will not be released in this case.
Then I will add the startup parameter-xmx256m, and this will be done.

I still don't know much about the principles of garbage collection. I checked it online and found several good articles.Article.

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

Also: the management of Java heap-garbage collection mentioned a few points, very good, may be used as the principle for writing a program:

(1) do not try to assume the time when the garbage collection occurred. This is all unknown. For example, a temporary object in a method becomes useless after the method is called, and its memory can be released.

(2) Java provides some classes dealing with garbage collection, and provides a method to forcibly execute garbage collection-call system. GC (), but this is also an uncertain method. Java does not guarantee that every time this method is called, it will be able to start garbage collection, but it will send such an application to JVM, whether or not to really execute garbage collection, everything is unknown.

(3) Select a suitable garbage collector. In general, if the system does not have special and demanding performance requirements, you can use the default JVM option. Otherwise, you can consider using targeted garbage collectors. For example, incremental collectors are suitable for systems with high real-time requirements. The system has high configuration and many idle resources. You can consider using parallel mark/clear collectors.

(4) The key problem is memory leakage. Good Programming habits and rigorous programming attitude are always the most important. Do not make yourself a small error and cause a large memory vulnerability.

(5) Release reference of useless objects as soon as possible.
When using temporary variables, most programmers automatically set the reference variable to null after exiting the scope. This implies that the Garbage Collector collects the object, you must also check whether the referenced object is monitored. If so, remove the listener and assign a null value.

That is to say, it is better to control the frequent memory application and memory release operations, but the system. the GC () method is not necessarily applicable. It is best to use finallize to force execution or write your own finallize method.

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

Tomcat error: Java. Lang. outofmemoryerror: Java heap space, so I checked the information and 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 are:
Java-xms32m-xmx128m

If you use win
Add the following command to/tomcat/bin/Catalina. BAT:
Set java_opts =-xms32m-xmx256m

If you use Unix/Linux
Add the following command to/tomcat/bin/Catalina. sh:
Java_opts = "-xms32m-xmx256m"

========================================================== =
Cause:

An exception occurs when a Java program is used to query a large amount of data from the database:
Java. Lang. outofmemoryerror: Java heap Space
This exception is thrown if 98% is used for GC and the available heap size is less than 2% in JVM.
JVM heap setting refers to the setting of memory space that JVM can allocate during Java program running. the JVM automatically sets the heap size value at startup. The initial space (-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-xmn-XMS-xmx provided by JVM to set the configuration.
Solution:
Catalina. BAT (WIN) or Catalina. Sh (Linux) in tomcat_home/binCodeAdd:
Set java_opts = % java_opts %-xms128m-xmx512m

========================================================== ==================================
Eclipse java. Lang. outofmemoryerror: Java heap Space Solution
========================================================== ==============================
Eclipse java. Lang. outofmemoryerror: Java heap Space Solution

The JVM size is set in the startup parameters of Eclipse, because JVM is also required during eclipse runtime, so eclipse. the JVM size set in INI is not the JVM size used when a specific program is running, which is irrelevant to the JVM size of the specific program.

So how can we set the JVM size of a program? (of course, this problem does not exist if the console runs, for example, Java-xms256m-xmx1024m classname, in this way, the JVM size of the current program can be set )?

Because the JVM configuration of the default program in eclipse is-xms8m-xmx128m, We need to manually adjust the memory consumption during processing so that the memory will not overflow. The specific setting method is as follows:

Select the running class and click 'run as-> open run dialog... ', select the VM arguments box under the (x) = argument tab, enter-xmx512m, and save and run it.

========================================================== ====================
Java. Lang. outofmemoryerror: Java heap space in eclipse

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

Add tomcat in Catalina. bat
Set java_opts =-xms256m-xmx512m-djava. AWT. Headless = true
256 and 512 are min and Max memory respectively.
In eclipse
Windows-> preferences...-> tomcat-> JVM...-> In the JVM text box, add-xms256m-xmx512m
That's all!

========================================
VM setting method in eclipse-[]-Tag: Java. Lang. outofmemoryerror: Java heap Space

Right-click and choose run.... In the pop-up window, enter the appropriate memory value in the VM input box.

========================================
In eclipse, yellow line is a warning.
Private 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 under eclipse, indicating that it is still referenced?
Here, the add method is not recommended.

Outofmemoryerror is irrelevant to the suspected reference. It may be because the Java memory is insufficient because the object is created cyclically.

Specify the memory of the Java Virtual Machine with parameters.
Java-xms256m-xmx1024m-XX: maxpermsize = 256 m

========================================================== ======================
2. If a memory overflow problem occurs during startup, the following exception information is thrown:
Java. Lang. outofmemoryerror: Java heap Space

You can try the following methods:
A. Modify Tomcat \ bin \ Catalina. BAT and add the following content:
Set java_opts =-xms256m-xmx512m-djava. AWT. Headless = true [-XX: maxpermsize = 128 M]

B. eclipse-> Windows-> preferences...-> tomcat-> JVM...-> JVM text box, add-xms256m-xmx512m

C. eclipse-> preference-> JAVA-> instal jres-> edit, add the parameter-xms256m-xmx512m

Reference reason: if 98% of the time in JVM is used for GC and is available, this exception is thrown when the heap size is less than 2%.
JVM heap setting refers to the setting of memory space that JVM can allocate during Java program running. the JVM automatically sets the heap size value at startup. The initial space (-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-xmn-XMS-xmx provided by JVM to set the configuration. The heap size should not exceed 80% of the available physical memory. Generally, the-XMS and-xmx options must be set to the same, and-xmn is the-xmx value of 1/4.
The-XMS-xmn setting of heap size should not exceed the physical memory size. Otherwise, the system prompts "error occurred during initialization of VM cocould not reserve enough space for object Heap ".

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

My causes:

Use spring + hibernate to read a large amount of data from the database, use the getbean of appcontext in singleton mode, and cache a large number of entity beans in the memory ......

Currently, there is no effective solution. by increasing the JVM memory size and modifying the Data Reading method (for example, only reading useful information), it is mitigated.

Modify the Data Reading method:
For example, an entitybean has the following fields: ID, name, age, address, and description. If only the ID and name fields of the bean are used in the program, read only the two fields from the database instead of the entire bean and the associated bean.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/jxzxm1_2/archive/2008/05/31/2499751.aspx

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.