Adjust JVM memory

Source: Internet
Author: User
Tags xms

Java. lang. OutOfMemoryError: memory size allocated during tomcat startup in Java heap space + myeclipse

This problem is mainly caused by java. lang. OutOfMemoryError: Java heap space. After such a problem occurs for the first time, it causes other problems. Check on the Internet may be the reason why JAVA's stack settings are too small.
There are two solutions to this problem:
1. Set Environment Variables
Set JAVA_OPTS =-Xms32m-Xmx512m
You can change the memory of your machine.

2. java-Xms32m-Xmx800m className
This parameter is added when the JAVA class file is executed. className indicates the class name to be executed. (Including package name)
This solves the problem. In addition, the execution speed is much faster than when no setting is made.

If you may use Eclispe during the test, you need to enter the-Xms32m-Xmx800m parameter in the VM arguments in Eclipse-> run-arguments.

Later, the startup parameter was modified in Eclilpse and-Xms32m-Xmx800m was added to the VM arguments to solve the problem.

1. java. lang. OutOfMemoryError: PermGen space

PermGen space stands for Permanent Generation space, which is the Permanent storage area of the memory,
This memory is mainly used by JVM to store Class and Meta information. When the Class is loaded, it will be placed in PermGen space,
Unlike the Heap region of the storage Instance, GC (Garbage Collection) does not
PermGen space is cleaned up. Therefore, if your application contains many classes, the PermGen space error may occur,
This error is common when the web server pre-compile the JSP. If your web app uses a large number of third-party jar files
This error message is generated when the default jvm size (4 MB) is exceeded.
Solution: manually set the MaxPermSize

Modify TOMCAT_HOME/bin/catalina. sh
Add the following lines to "echo" Using CATALINA_BASE: $ CATALINA_BASE:
JAVA_OPTS = "-server-XX: PermSize = 64 M-XX: MaxPermSize = 128 m
Suggestion: Move the same third-party jar files to the tomcat/shared/lib directory to reduce the memory usage of jar files.

Ii. java. lang. OutOfMemoryError: Java heap space
Heap size settings
The JVM Heap setting refers to the setting of the memory space that JVM can allocate during the 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.
. The Heap size is the sum of Young Generation and Tenured Generaion.
Tip: if 98% is used for GC and the available Heap size is less than 2% in JVM, this exception is thrown.
Tip: the Heap Size should not exceed 80% of the available physical memory. Generally, you must set the-Xms and-Xmx options to the same, and-Xmn to the-Xmx value of 1/4.
Solution: manually set Heap size
Modify TOMCAT_HOME/bin/catalina. sh
Add the following lines to "echo" Using CATALINA_BASE: $ CATALINA_BASE:
JAVA_OPTS = "-server-Xms800m-Xmx800m-XX: MaxNewSize = 256 m"

Iii. Example:

JAVA_OPTS = "-server-Xms800m-Xmx800m-XX: PermSize = 64 M-XX: MaxNewSize = 256 m-XX: MaxPermSize = 128 m-Djava. awt. headless = true"

For a large web project, the memory space allocated by tomcat by default cannot be started. If you do not start tomcat in myeclipse, you can set tomcat as follows:

TOMCAT_HOME/bin/catalina. bat adds the following sentence:

Set JAVA_OPTS =-Xmx1024M-Xms512M-XX: MaxPermSize = 256 m

If you want to start it in myeclipse, the above modification will not work. You can set it as follows:

Myeclipse-> preferences-> myeclipse-> servers-> tomcat ×. ×-> In the JDK panel

Add-Xmx1024M-Xms512M-XX: MaxPermSize = 256 m to Optional Java VM arguments.

 

2/===================================================

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.
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.

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:
In tomcat_home/bin, add:
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 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.

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.