Java Memory Overflow

Source: Internet
Author: User

One, memory overflow type

1. Java.lang.OutOfMemoryError:PermGen Space

The JVM manages two types of memory, heap, and non-heap. The heap is intended for developers, and is created at the start of the JVM, and the non-heap is left to the JVM for its own use to store the information for the class. Unlike heaps, the GC does not free up space during the run time. If the Web app uses a large number of third-party jars, or if the application has too many class files, and the MaxPermSize setting is small, exceeding this will cause the memory to overflow too much, or the Tomcat hot deployment will not clean up the previously loaded environment, Only the context is changed to the newly deployed, and the non-stockpiled content becomes more and more.

PermGen space is the full name of permanent Generation space, refers to the memory of the permanent storage area, this block of memory is mainly stored by the JVM class and meta information, class is loader will be placed in PermGen Space, unlike the heap area where the class instance (Instance) is stored, the GC (garbage Collection) does not clean up permgen space during the main program run time, so if you have a class in your application, PermGen space errors are most likely to occur when the Web server pre-compile the JSP. If you have a large number of third-party jars under your web app that are larger than the JVM's default size (4M), this error message will be generated.

One of the best configuration examples:

Set java_opts=-xms800m-xmx800m-xx:permsize=128m-xx:maxnewsize=256m-xx:maxpermsize=256m

2. Java.lang.OutOfMemoryError:Javaheap Space

The first case is a supplement, and the main problem is that it appears in this situation. Its default space (that is,-XMS) is 1/64 of physical memory, and the maximum space (-XMX) is 1/4 of physical memory. If the memory is less than 40%,JVM the value of the heap to the XMX setting is increased, the memory remaining more than 70%,JVM will reduce the heap to XMS setting value. So the server's xmx and XMS settings should generally be set identically to avoid resizing the virtual machine heap after each GC. Assuming that the physical memory is infinite, then the maximum size of the JVM memory is related to the operating system, the general 32-bit machine is between 1.5g and 3g, and 64-bit will not be limited.

Note: If XMS exceeds the XMX value, or if the sum of the heap maximum and the non-heap maximum exceeds the physical memory or the maximum operating system limit, the server will not start up.

Garbage collection GC:

The frequency with which the JVM calls the GC is still high, with garbage collection in two main cases:

When the application thread is idle, and the other is that the Java memory heap is not sufficient, the GC is constantly called, and if continuous recycling does not solve the problem of insufficient memory heap, it will report an out-of-storage error. Because this exception is determined by the operating environment of the system, it cannot be expected when it appears. According to the GC mechanism, the operation of the program can cause changes in the operating environment of the system and increase the chance of the GC triggering. To avoid these problems, the design and writing of the program should avoid the garbage object's memory footprint and the GC overhead. The display call System.GC () can only suggest that the JVM needs to recycle the garbage object in memory, but it does not have to be recycled at once, one that does not solve the memory depletion situation, and also increases GC consumption.

Second, the JVM memory area composition

Java has two kinds of memory: one is stack memory, the other is heap memory

The basic type variables and object reference variables defined in the function are allocated in the stack memory of the function, and heap memory is used to hold the objects and arrays created by new. When a variable is defined in a function (code block), Java allocates memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees up the allocated memory space for that variable, and the memory allocated in the heap is managed by the Java Virtual machine's automatic garbage collector. The advantage of a heap is that it can dynamically allocate memory size, and the lifetime does not have to tell the compiler beforehand because it allocates memory dynamically at run time. The disadvantage is to dynamically allocate memory at run time, the access speed is slow, the advantage of the stack is faster than the heap, the disadvantage is that there is a stack of data size and lifetime must be determined without flexibility.

The Java heap is divided into three zones: New, old, and permanent

The GC has two threads: the newly created object is assigned to the new zone, and when the area is filled, it is moved to the old area by the GC worker thread, and when the old area is filled, all objects in the heap memory that trigger the GC main thread traverse. The size of the old area equals Xmx minus-xmn.

Stack adjustment: Parameters are +usedefaultstacksize-xss256k, indicating that each thread can request 256k of stack space. Each thread has his own stack.

Iii. how the JVM sets up virtual memory

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

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

Tip: The initial memory allocated by the JVM is specified by-XMS, which defaults to 1/64 of the physical memory; The maximum allocated memory for the JVM is specified by-XMX, which defaults to 1/4 of the physical memory.

The JVM uses-xx:permsize to set the non-heap memory initial value, which defaults to 1/64 of the physical memory;

The maximum amount of non-heap memory is set by Xx:maxpermsize, which defaults to 1/4 of physical memory.

Workaround: Manually set the heap size

Modify Tomcat_home/bin/catalina.bat

Add the following line to the "echo" Using catalina_base: $CATALINA _base "":

Java_opts= "-server-xms800m-xmx800m-xx:maxnewsize=256m"

 

Four, the characteristics of the non-robust code and solutions

1. Release the reference of the useless object as soon as possible. A good idea is to use a temporary variable when the reference variable is automatically set to null after exiting the active domain, implying that the garbage collector collects the object to prevent a memory leak.

For instances where pointers are still pointing, the JVM does not reclaim the resource because garbage collection will use null-valued objects as garbage, increasing the efficiency of the GC recovery mechanism;

2, our program is inevitably a lot of string processing, avoid the use of string, should use a large number of StringBuffer, each string object has to occupy an area of memory;

String str = "AAA";

String str2 = "BBB";

String STR3 = str + str2;//If you do this after STR, STR2 is not called later, it will be placed in memory waiting for the Java GC to be recycled, too much in the program such a situation will report the above error, It is recommended that you use StringBuffer when using strings, so you can save a lot of overhead by using string.

3, minimize the use of static variables, because the static variables are global, the GC will not be recycled;

4, avoid the centralized creation of objects, especially large objects, the JVM will suddenly need a lot of memory, it will inevitably trigger the GC to optimize the system memory environment, display the Declaration array space, and the number of applications is also very large.

5, try to use the object pool technology to improve the system performance; objects with long life cycles are prone to memory leaks when they have short life-cycle objects, such as when large collection objects have large data volumes of business objects, consider chunking and then resolve a piece of policy that frees a piece.

6. Do not create objects in frequently invoked methods, especially when creating objects in loops. You can use Hashtable,vector to create a set of object containers and then fetch those objects from the container without discarding them each time you new

7, generally occurs in the opening of large files or with the database once took too much data, resulting in the state of the memory Error, it is likely to calculate the maximum amount of data, and set the required minimum and maximum memory space value.

Finally: This article is transferred from the "Bit network", reproduced please keep the original link: http://soft.chinabyte.com/database/33/11679033.shtml.



Java Memory Overflow

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.