Several memory overflows and solutions common to Java

Source: Internet
Author: User
Tags xms

1.JVM Heap Overflow: Java.lang.OutOfMemoryError:Java heap Space

The JVM automatically sets the value of the JVM heap when it is started, and can be set using options such as the-XMN-XMS-XMX provided by the JVM. The size of the heap is the sum of young Generation and tenured generaion. 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%.
Workaround: Manually set the size of the JVM heap (heap).

The Java heap is used to store object instances. When you need to allocate memory for an object instance, the memory footprint of the heap has reached the maximum value set by the-XMX. A OutOfMemoryError exception will be thrown. Examples are as follows:

 Packagecom.demo.test;Importjava.util.ArrayList;Importjava.util.List;/*** VM Args:-xms5m-xmx5m*/ Public classHeapoom { Public Static voidMain (string[] args) {intCount = 0; List<Object> list =NewArraylist<object>();  while(true) {List.add (NewObject ()); System.out.println (++count); }    }}

Then set the JVM parameters at run time, as follows:

The-XMX is 5m. One of the test results is that when the value of count is accumulated to 360145, the following exception occurs:

Exception in thread ' main ' Java.lang.OutOfMemoryError:Java heap space at    java.util.Arrays.copyOf (Arrays.java: 2245) at    java.util.Arrays.copyOf (arrays.java:2219) at    Java.util.ArrayList.grow ( Arraylist.java:213) at    java.util.ArrayList.ensureCapacityInternal (arraylist.java:187) At    Java.util.ArrayList.add (arraylist.java:411) at    Com.demo.test.HeapOOM.main (Heapoom.java: 12)

Modify-xmx to 10m. One of the tests results in a OutOfMemoryError exception that occurs when the value of count is incremented to 540217. As the value of the-XMX parameter increases, more objects can be stored in the Java heap.

2.PermGen space overflow: Java.lang.OutOfMemoryError:PermGen space

The full name of PermGen space is permanent Generation space, which refers to the permanent storage area of memory. Why memory overflow, this is because the memory is mainly stored by the JVM class and meta information, class in the load is loaded into the PermGen space area, which is stored in the instance heap area, sun The GC does not clean up permgen space during the main program's run time, so if your app loads many classes, there's a good chance that permgen space will overflow. Typically occurs during the startup phase of a program.
Workaround: Set the permanent generation size by-xx:permsize and-xx:maxpermsize.

The method area is used to hold information about the Java type, such as the class name, access modifier, Chang, field description, method description, and so on. As the class loader loads the class file into memory, the virtual opportunity extracts the type information and stores the information in the method area. The OutOfMemoryError exception is thrown when the class information needs to be stored and the memory footprint of the method area has reached the maximum value set by the-xx:maxpermsize. The basic idea for testing this scenario is that the runtime generates a large number of classes to fill the method area until it overflows. There are a lot of dynamic classes that need to be run with Cglib directly to manipulate bytecode. Examples are as follows:

 Packagecom.demo.test;ImportJava.lang.reflect.Method;ImportNet.sf.cglib.proxy.Enhancer;ImportNet.sf.cglib.proxy.MethodProxy;ImportNet.sf.cglib.proxy.MethodInterceptor;/*** VM Args:-xx:permsize=10m-xx:maxpermsize=10m*/ Public classMethodareaoom { Public Static voidMain (string[] args) {intCount = 0;  while(true) {Enhancer enhancer=Newenhancer (); Enhancer.setsuperclass (methodareaoom.class); Enhancer.setusecache (false); Enhancer.setcallback (NewMethodinterceptor () { PublicObject Intercept (Object obj, Method method, object[] args, Methodproxy proxy)throwsThrowable {returnproxy.invoke (obj, args);            }            });            Enhancer.create (); System.out.println (++count); }    }}

The-xx:maxpermsize is 10m. One of the test results is that when the value of count is accumulated to 800, the following exception occurs:

caused By:java.lang.OutOfMemoryError:PermGen space at    java.lang.ClassLoader.defineClass1 (Native Method) At    Java.lang.ClassLoader.defineClass (classloader.java:792)     8 more

As the value of the-xx:maxpermsize parameter increases, more types of data can be stored in the Java method area.

3. Stack overflow: java.lang.StackOverflowError:Thread stack space

Stack overflows, the JVM is still a stack-based virtual machine, which is the same as C and Pascal. The procedure for calling a function is on the stack and on the fallback. There are too many "layers" of calls to the constructor to overflow the stack area. Generally speaking, the general stack area is much smaller than the heap area, because the function call process is often not more than thousands of layers, and even if each function call requires 1K of space (this is approximately equivalent to a C function declared 256 int type variable), then the stack area is just need 1MB of space. The size of the stack is usually 1-2mb. The popular point is that single-threaded programs require too much memory. Often recursion does not have too many levels of recursion, it is easy to overflow.
WORKAROUND: 1: Modify the program. 2: Use-XSS: To set the stack size for each thread.

In the Java Virtual Machine specification, two exceptions are specified for this zone: Stackoverflowerror and OutOfMemoryError exceptions.

(1) Stackoverflowerror anomalies

Each time the Java program code launches a new thread, the Java Virtual machine assigns it a Java stack. The Java stack saves the running state of a thread in frames. When a thread calls a Java method, the virtual machine presses a new stack frame into the thread's Java stack. As long as this method has not returned, it has been there. If a thread has too many nested call hierarchies (such as recursive calls), as the number of frames in the Java stack grows, it will end up with a stackoverflowerror memory overflow exception due to the sum of all the stack frame sizes in the thread's Java stack greater than the value set by-XSS. Examples are as follows:

 Packagecom.demo.test;/*** VM Args:-xss128k*/ Public classjavavmstacksof {Private intCount = 0;  Public Static voidMain (string[] args) {Newjavavmstacksof (). method (); }         Public voidmethod () {System.out.println (++count);    Method (); }}

The-XSS is 128k. One of the test results is that when the value of count is accumulated to 2230, the following exception occurs:

Exception in thread "main"Java.lang.StackOverflowError at Sun.nio.cs.UTF_8.updatePositions (Utf_8.java:77) at Sun.nio.cs.utf_8$encoder.encodearrayloop (Utf_8.java:564) at Sun.nio.cs.utf_8$encoder.encodeloop (Utf_8.java:619) at Java.nio.charset.CharsetEncoder.encode (Charsetencoder.java:36w) at Sun.nio.cs.StreamEncoder.implWrite (Streamencoder.java:271) at Sun.nio.cs.StreamEncoder.write (Streamencoder.java:125) at Java.io.OutputStreamWriter.write (Outputstreamwriter.java:207) at Java.io.BufferedWriter.flushBuffer (Bufferedwriter.java:129) at Java.io.PrintStream.write (Printstream.java:526) at Java.io.PrintStream.print (Printstream.java:597) at Java.io.PrintStream.println (Printstream.java:736) at Com.demo.test.JavaVMStackSOF.method (Javavmstacksof.java:15)

As the value of the-XSS parameter increases, the level of method calls that can be nested increases accordingly. In summary, the STACKOVERFLOWERROR exception occurs because the hierarchy of the method call is too deep, resulting in a stackoverflowerror exception because the sum of all the stack frame sizes assigned to a thread is greater than the value set by the-XSS.

(2) OutOfMemoryError anomalies

When the Java program code starts a new thread, there is not enough memory space for the thread to allocate the Java stack (the size of a thread's Java stack is determined by the-XSS parameter), and the JVM throws a OutOfMemoryError exception. Examples are as follows:

 Packagecom.demo.test;/*** VM Args:-xss128k*/ Public classJavavmstackoom { Public Static voidMain (string[] args) {intCount = 0;  while(true) {thread thread=NewThread (NewRunnable () { Public voidrun () { while(true) {                        Try{Thread.Sleep (5000); } Catch(Exception e) {}}}            });            Thread.Start (); System.out.println (++count); }    }}

The-XSS is 128k. One of the test results is that when the value of count is accumulated to 11958, the following exception occurs:

New native thread at    java.lang.Thread.start0 (Native Method) at    Java.lang.Thread.start (thread.java:693) At    Com.demo.test.JavaVMStackOOM.main (Javavmstackoom.java:21)

As the value of the-XSS parameter increases, the number of bus threads that the Java program can create is lower.

4. So when the server container starts, we often care about and set several parameters of the JVM as follows:

The initial size of the-xms:java heap, which defaults to 1/64 of the physical memory.
-xmx:java Heap Maximum, no more than physical memory.
The heap size of the-xmn:young generation is typically set to one of the 3, 4 points of xmx. Increasing the younger generation will reduce the size of older generations and can be reasonably set according to monitoring.
-XSS: The stack size of each thread, and the best value should be 128K, the default value seems to be 512k.
-xx:permsize: Sets the initial size of the memory's permanent save, the default value is 64M.
-xx:maxpermsize: Sets the maximum size of the memory's permanent save, the default value is 64M.
The ratio of the size of the-xx:survivorratio:eden area to the Survivor area is set to 8, then the ratio of two survivor to one Eden area is 2:8, and one survivor area represents 1/10 of the entire young generation.
-xx:+useparallelgc:f younger generations use concurrent collections, while older generations still use serial collections.
-XX:+USEPARNEWGC: Set the young on behalf of the parallel collection, JDK5.0 above, the JVM will be set according to the system configuration itself, there is no need to set this value.
-xx:parallelgcthreads: The number of threads in the parallel collector, the best value is configured equal to the number of processors for the CMS.
-XX:+USEPARALLELOLDGC: Old Generation garbage collection method for parallel collection (Parallel compacting).
-xx:maxgcpausemillis: The maximum time for each young generation garbage collection (max pause time), if this time is not met, the JVM automatically adjusts the younger generation size to meet this value.
-xx:+scavengebeforefullgc:full called YGC before GC, the default is true.

Real example: java_opts= "-xms4g-xmx4g-xmn1024m-xx:permsize=320m-xx:maxpermsize=320m-xx:survivorratio=6″

The first kind of outofmemoryerror:permgen space

The original intention of this problem is that the program uses a large number of jar or class, so that the Java Virtual machine load class space is not enough, and permanent Generation space. There are two ways to address this type of problem:

1, increase the size of the xx:permsize and xx:maxpermsize parameters in the Java Virtual machine, where xx:permsize is the initial permanent save area size, Xx:maxpermsize is the largest permanent save area size. For tomcat6.0, add a line to the end of the list of environment variable names in the catalina.sh or Catalina.bat file (approximately 70 rows or so): java_opts= "-XX:PERMSIZE=64M-XX: Maxpermsize=128m "If you are a Windows server, you can also set it in the system environment variable. This memory overflow error can easily occur when using Tomcat to publish Sprint+struts+hibernate schema programs. Using the above method, I successfully solved the problem of frequent outages of the Tomcat server that deployed the SSH project.

2, clean the jar under Web-inf/lib in the application, if Tomcat deploys multiple applications, many applications use the same jar, and can move the common jar to the Tomcat common Lib, reducing the repetition load of the class. This method is recommended by some people on the Internet, I have not tried, but feel less space, the most reliable is the first method.

The second type of Outofmemoryerror:java heap space

This problem occurs because there are too many objects created by the Java Virtual machine, and the virtual machine allocates enough memory space to the heap for garbage collection, which is related to heap space. There are two ways to solve this kind of problem:

1, check the program to see if there is a dead loop or unnecessarily repeatedly create a large number of objects. After you find the cause, modify the program and algorithm. I used to write a K-means text clustering algorithm for tens of thousands of of text records (the eigenvector of each record about 10 or so) for text clustering, due to procedural details of the problem, led to the Java heap space memory overflow problem, later through the modification program was resolved.

2. Increase the size of the XMS (initial heap size) and XMX (maximum heap size) parameters in the Java Virtual machine. such as: Set java_opts=-xms256m-xmx1024m

Several memory overflows and solutions common to Java

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.