Java memory overflow

Source: Internet
Author: User
Tags xms jprofiler

I. memory overflow type
1. java. lang. OutOfMemoryError: PermGen space
JVM manages two types of memory, heap and non-heap. Heap is used by developers. It is created at JVM startup. Non-heap is reserved for JVM and used to store class information. Unlike the heap, GC does not release space during runtime. If the web app uses a large number of third-party jar files or the application has too many class files, but the MaxPermSize setting is small, exceeding this will also cause excessive memory usage and overflow, or during hot deployment of tomcat, the Environment loaded above will not be cleaned up, but the context will be changed to a new deployment, so there will be more and more non-heap content.

2. java. lang. OutOfMemoryError: Java heap space
The first case is a supplement. The main problem is that it appears in this case. The default space (-Xms) is 1/64 of the physical memory, and the maximum space (-Xmx) is 1/4 of the physical memory. If the remaining memory is less than 40%, the JVM will increase the heap to the Xmx setting value. If the remaining memory exceeds 70%, the JVM will reduce the heap to the Xms setting value. Therefore, the Xmx and Xms settings of the server should be set to the same value to avoid adjusting the size of the VM heap after each GC operation. If the physical memory is infinitely large, the maximum JVM memory size is related to the operating system. Generally, 32-bit machines are between GB and 3 GB, and 64-bit machines are not limited.

Note: If the Xms value exceeds the Xmx value, or the sum of the heap maximum value and the non-heap maximum value exceeds the physical memory or the maximum operating system limit, the server cannot be started.

Garbage collection GC role

JVM calls GC frequently. Garbage collection is mainly performed in two cases:

When the application thread is idle, and when the java memory heap is insufficient, GC is continuously called. If continuous collection fails to solve the problem of insufficient memory heap, an out of memory error is reported. This exception is determined by the system running environment, so it cannot be expected when it will appear.

According to the GC mechanism, program running may cause changes in the system runtime environment and increase the chance of GC triggering.

To avoid these problems, the program design and writing should avoid the memory usage and GC overhead of spam objects. It is shown that calling system. GC () can only be recommended that JVM recycle junk objects in memory, but not immediately,

One is that it cannot solve the problem of insufficient memory resources and increase GC consumption.

Ii. JVM memory zone Composition
Java stack and stack
Java divides memory into two types: stack memory and heap memory.

1. The basic type variables defined in the function and the referenced variables of the object are allocated in the function stack memory;

2. heap memory is used to store 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. When the scope of the variable is exceeded, java Automatically releases the memory space allocated for the variable. The memory allocated in the heap is managed by the Java Virtual Machine's automatic garbage collector. The advantage is that the memory size can be dynamically allocated, you do not need to tell the compiler about the lifetime because it dynamically allocates memory at runtime. The disadvantage is that the memory needs to be dynamically allocated at runtime, And the access speed is slow. The advantage of stack is that the access speed is faster than the heap speed, the disadvantage is that the data size and lifetime in the stack must be definite and inflexible.

Java heap is divided into three areas: new, old, and permanent.
GC has two threads:
The newly created object is allocated to the new area. When the area is filled, it will be moved to the old area by the GC auxiliary thread, when the old area is full, the main GC thread is triggered to traverse all objects in the heap memory. The size of the old area is equal to xmx minus-xmn.

Java stack Storage
Stack adjustment: The parameter usedefastackstacksize-xss256k indicates that each thread can apply for a stack space of kb.
Each thread has its own stack

Iii. How to Set virtual memory in JVM
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.

Tip: the initial memory allocated by JVM is specified by-Xms. The default value is 1/64 of the physical memory. The maximum memory allocated by JVM is specified by-Xmx. The default value is 1/4 of the physical memory.
By default, when the free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of-Xmx. When the free heap memory is greater than 70%, the JVM will reduce the minimum limit of heap until-Xms. Therefore, the server generally sets-Xms and-Xmx to be equal to each other to avoid adjusting the heap size after each GC.

Tip: if the physical memory is infinitely large, the maximum JVM memory has a great relationship with the operating system.
Simply put, although the 32-bit processor has a controllable memory space of 4 GB, the specific operating system will impose a limit,
This limit is generally 2 GB-3 GB (1.5 GB-2 GB in Windows and 2 GB-3 GB in Linux ), the 64-bit and above processors will not be limited.

Tip: Note: If the Xms value exceeds the Xmx value, or the sum of the heap maximum value and the non-heap maximum value exceeds the physical memory or the maximum operating system limit, the server cannot be started.

Tip: Set NewSize and MaxNewSize to be equal. The size of "new" should not be greater than half of "old" because if the old area is not large enough to frequently trigger "Main" GC, greatly reduced performance
JVM uses-XX: PermSize to set the non-heap memory initial value. The default value is 1/64 of the physical memory;
Set the maximum non-heap memory size by XX: MaxPermSize. The default value is 1/4 of the physical memory.

Solution: manually set Heap size
Modify TOMCAT_HOME/bin/catalina. bat
Add the following lines to "echo" Using CATALINA_BASE: $ CATALINA_BASE:
JAVA_OPTS = "-server-Xms800m-Xmx800m-XX: MaxNewSize = 256 m"

Iv. Use of performance check tools
Locate Memory leakage:
JProfiler is mainly used to check and track the performance of the system (limited to Java Development. JProfiler can monitor the system's memory usage and garbage collection and thread running status at any time to monitor the JVM running status and performance.
1. The memory of the application server is used unreasonably for a long time. The memory is often occupied at a high level, which is hard to be recycled to a low level;

2. The application server is extremely unstable and restarts every two days, sometimes even every day;

3. the Application Server often performs Full GC (Garbage Collection), which takes about 30-40 seconds. When performing Full GC, the application server does not respond to the customer's transaction request, it has a significant impact on system performance.
Because the development environment and product environment are different, this problem sometimes occurs in the product environment. Generally, you can use tools to track the memory usage of the system, in some cases, a large amount of memory may be used at some time, resulting in out of memory. In this case, we should continue to track whether there will be any decrease in the future, if it remains high, it is certainly because the program causes memory leakage.

5. Features and solutions for unrobust code
1. Release reference of useless objects as soon as possible. A good way is to enable the reference variable to be automatically set to null after exiting the active domain when using a temporary variable, suggesting that the Garbage Collector collects the object to prevent memory leakage.
For instances with pointers, jvm will not recycle this resource, because garbage collection will take null objects as garbage, improving the efficiency of GC collection mechanism;

2. In our program, a large amount of String processing is inevitable. To avoid the use of String, a large number of StringBuffer should be used. Each String object must occupy an independent area of memory;
String str = "aaa ";
String str2 = "bbb ";
String str3 = STR str2; // If STR and str2 are not called after this execution, it will be stored in the memory and waited for Java GC to be recycled, if this happens too many times in the program, the above error will be reported. We recommend that you use stringbuffer when using strings instead of using strings, which saves a lot of overhead;

3. Use as few static variables as possible, because static variables are global and GC will not be recycled;

4. Avoid creating objects in a centralized manner, especially large objects. JVM will suddenly need a large amount of memory, which will inevitably trigger GC to optimize the system memory environment. The declared array space is displayed, and the number of applications is large.
This is a case for your consideration.
Java. outofmemoryerror often occurs during File Upload using jspsmartupload,
Check and find the problem: code in the component
M_totalbytes = m_request.getcontentlength ();
M_binarray = new byte [m_totalbytes];
The cause is that the totalbytes variable has a very large number, resulting in a lot of memory space allocated to the array, and the array cannot be released in time. The solution can only be used in a more appropriate way. At least it will not cause an outofmemoryerror.

5. Try to use the object pool technology to improve system performance. objects with a long life cycle have objects with a short life cycle, which may cause memory leakage, for example, when a large collection object has a large data volume of business objects, you can consider processing them in multiple parts and then solve the policy of releasing one piece.

6. Do not create objects in frequently called methods, especially avoid creating objects in loops. You can use hashtable as appropriate. The vector creates a group of object containers and then retrieves those objects from the containers without dropping them after each new operation.

7. It usually occurs when a large file is opened or too much data is taken with the database at a time, resulting in an out of memory error. In this case, the maximum data volume is calculated, set the minimum and maximum memory space values.

Address: http://www.7747.net/kf/201010/76204.html

Related Article

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.