Possible causes and solutions for string memory overflow exceptions (errors)

Source: Internet
Author: User
Tags xms

Abstract: This blog mainly describes the possible causes and solutions of Java. Lang. outofmemoryerror: permgenspace. Permgen space is short for permanent generationspace, which indicates the permanent storage area of the memory. This memory is mainly used by JVM to store class and meta information, when a class is loaded, it will be placed in permgenspace, which is different from the heap region where the class instance is stored. GC (garbage collection) permgen space is not cleaned up during the main program running period. Therefore, if your application contains many classes, the permgen space error may occur.

 

Introduction:

Part 1: Principle Analysis (why is this happening)

Part 2: Practical Application Analysis and Solution reference

Part 3: How to Set java_opts for Tomcat

Part 4: How to Set Tomcat memory size in eclipse to solve memory overflow during startup

 

Part 1: Principle Analysis:

1,There is no limit on the string, but when the string is too large and exceeds the JVM's memory, it will throwJava. Lang. outofmemoryerrorError. There is no length limit for string.JVM memoryThe length of string is limited. At the same time, it means that no exception is thrown, but only an error is thrown.OutmemoryerrorIt indicates that the program is poorly designed or has encountered a large volume of data that exceeds the programmer's expectation.

In either case, there are only the following solutions. They are:

A. The designer re-designs the program and does not cause the program to load all the data at a time.

B. Data can be divided into smaller blocks.

C. Allocate more memory to the program.

D. Provide more memory for the Java Virtual Machine.

 

2,GenerallyEnable large archives or load too much data with the database at a time, Causing an out of memoryerror. In this case, the maximum amount of data is calculated and the minimum and maximum memory space values are set. In another situation, it is difficult to check. YesServlet or JSP Container was suddenly launched at the same timeThis also causes insufficient memory requirements. Therefore, you must calculate the amount of data required by the Program * Number of online users at the same time to set the memory demand. (If you want to optimize the value, it is best to adjust it with GC)

 

3,The faster the CPU speed, the larger the minimum demand for memory. The reason is that the faster the CPU, the larger the amount of data that can be processed in a short period of time. Therefore, before Java GC, the memory may have been exhausted, therefore, the CPU speed is also an important factor in the initial memory demand.

 

Solution:

ModifyJava_opts = "-Xms1024m-Xmx1024m"Or larger, depending on the system memory

Java. Lang. outofmemoryerror generation and solution in JSP development

 

Part 2: reference for actual application scenarios and solutions (server version-extract version ):

Pre-preparation (File Location in various environments)

[Windows] added in the first line of Catalina. BAT:

Set java_opts =-xms512m-xmx900m-XX: permsize = 128 M-XX: maxnewsize = 256 m-XX: maxpermsize = 256 m

[Non-Windows] added in the first line of Catalina. sh:
Java_opts =-xms512m-xmx900m-XX: permsize = 128 M-XX: maxnewsize = 256 m-XX: maxpermsize = 256 m

 

1. java. Lang. outofmemoryerror: permgenspace

Permgen space stands for permanentgeneration space, which is the permanent storage area of the memory,
This memory is mainlyClass and meta information stored by JVMThe class will be put into the permgen space when it is loader. It is different from the heap region where the class instance is stored,GC (garbage collection) does not clean the permgen space during the main program running period. Therefore, if your application has many classes, the permgenspace error may occur.This error is common when the web server precompile JSP. If your web app uses a large number of third-party jar files and the size exceeds the default JVM size (4 MB), this error message is generated.


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:
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 is the sum of young generation and tenured generaion.
Tip 1:This exception is thrown if 98% is used for GC and the available heap size is less than 2% in JVM.
Tip 2: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.


Solution: manually set heap size
Modify tomcat_home/bin/Catalina. Sh
Add the following lines to "Echo" usingcatalina_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 = 256m-xx: maxpermsize = 128 M-djava. AWT. Headless = true"

As we all know, when a Java program is started, the JVM will allocate an initial memory and the maximum memory to this application. The initial memory and maximum memory will affect the performance of the program to a certain extent. For example, when applications use the maximum memory, the JVM needs to first perform garbage collection to release some occupied memory.
Therefore, to adjust the initial memory and maximum memory at Tomcat startup, you must declare to JVM, generally, when running Java programs, you can use mid-XMS-xmx to adjust the initial memory and maximum memory of the application:
For example, Java-xms64m-xmx128m A. jar.
Tomcat startup programs are packaged and cannot be directly used to change the memory Settings Using Java-X... tomcat.

There are two ways to change this setting in Tomcat:
1. Add the tomcat_opts and catalina_opts attributes to the environment variable,
For example, set catalina_opts =-xms64m-xmx512m;
Ms is the smallest, MX is the largest, 64 m, m respectively refers to the memory capacity.

2. Modify the Catalina. BAT file
In line 1, "REM execute Java with the applicable properties ",
% _ Execjava % java_opts % catalina_opts % debug_opts %-djava. endorsed. dirs = "% java_endorsed_dirs %"-classpath "% classpath %"-dcatalina. base = "% catalina_base %"-dcatalina. home = "% catalina_home %"-djava. io. % catalina_opts % in tmpdir = "% catalina_tmpdir %" % mainclass % cmd_line_args % action % to-Xms64m-Xmx512m

 

 

Part 3: How to Set java_opts for Tomcat installed

Java_opts = "-Xms128M-Xmx512M-XX: permsize = 128 M-XX: maxpermsize = 512 M"

If Tomcat is started through the windows service, it executes bin \ tomcat.exe. It reads the value in the registry, instead of setting Catalina. bat.
Solution:
Modify the Registry HKEY_LOCAL_MACHINE \ SOFTWARE \ Apache Software Foundation \ procrun2.0 \ tomcat6 \ Parameters \ javaoptions
Original Value:
-Dcatalina. Home = E: \ Tomcat 6.0.
-Dcatalina. base = E: \ Tomcat 6.0.
-Djava. endorsed. dirs = E: \ Tomcat 6.0 \ common \ endorsed
-Djava. Io. tmpdir = E: \ Tomcat 6.0 \ Temp
-Djava. util. Logging. Manager = org. Apache. Juli. classloaderlogmanager

-Djava. util. Logging. config. File = E: \ atat6.0 \ conf \ Logging. Properties

Add the following content:

-Xms128m

-Xmx512m

-XX: permsize = 128 m

-XX: maxpermsize = 512 m

It must be written by a branch.

The final result is as follows: (add the corresponding information to the back of the Java options input box)


Part 4: Set Tomcat memory size in eclipse to solve memory overflow during startup

Sometimes, startup of tomcat in eclipse may cause memory overflow and conversion errors, which may be caused by insufficient memory, in this case, you can find the server in the settings and find tomcat to find the corresponding version.

 

Method 1: Set the JVM parameter value permanently:

Find the append to jvmparameters in JVM settings in tomcat in preferences in windows and set the corresponding parameters:

-Xms128m-xmx512m-XX: permsize = 256m-xx: maxnewsize = 128 M-XX: maxpermsize = 512 m

As shown in:


Method 2: set the parameters temporarily. In eclipse, perform the following steps:

1. Click the drop-down arrow next to the debug icon on Eclipse.

2. Select debug configurations,

3. The tomcat configuration page is displayed. append the VM arguments parameter to the end of argument:

-Xms256M-Xmx512M-XX: permsize = 256 m-XX: maxpermsize = 512 m

Parameter meaning
-Vmargs: Specifies the VM parameters.
-Xms40m: the virtual machine occupies the Minimum Memory of the system.
-Xmx256m: the virtual machine occupies the maximum memory of the system.
-XX: permsize: Minimum stack memory size. Generally, when the memory is insufficient, it is said that this is too small. If the remaining heap space is smaller than 5%, a warning will be given. We recommend that you set this slightly larger, but it depends on the memory size of your machine.
-XX: maxpermsize: Maximum stack memory size. This should also be larger.
-5% of xmx512m is 25.6 m. Theoretically, the value of-xmx and-XX: maxpermsize must be greater than 25.6 m.


References:

Http://blog.csdn.net/hdfyq/article/details/5856284

Http://blog.csdn.net/w420372197/article/details/7878404

Http://piranha.iteye.com/blog/1574040

Http: // 116.20.173.144 /? C = article & A = read & id = 59498

Http://my.oschina.net/colorleaf/blog/175581

Possible causes and solutions for string memory overflow exceptions (errors)

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.