JVM memory analysis and unrobust code that causes memory overflow and Solutions

Source: Internet
Author: User
Tags xms

From: http://hi.baidu.com/fvsd3/blog/item/9baec09a301bdebcc9eaf4a2.html

I. JVM memory area composition

Java divides memory into four types:
1. Stack segment: automatically assigned and released by the compiler, stores function parameter values, local variable values, and so on. After the specific method is executed, the system automatically releases memory resources.
2. Heap segment-it is usually assigned and released by programmers to store the objects and arrays created by new. The JVM can view this object from time to time. If no reference is made to this object, it will be recycled.
3. Data Segment-stores global variables, static variables, and string constants.
4. Code segment: stores the binary code of methods in the program, and multiple objects share a code space area.

When a variable is defined in a method (code block), Java allocates memory space for the variable in the stack. When the variable's scope 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 of the heap 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.

The Java heap consists of the perm and heap areas, and the heap area consists of the old and new areas. The new areas are divided into Eden areas, from areas, to areas, heap = {old + new = {Eden, from, to}, as shown in figure 1.

Heap distinguishes two major blocks: New Generation and old generation. in newgeneration, there is a space called Eden, mainly used to store new objects, and there are two consumer vor spaces (from, ), they are used to store objects that survive each garbage collection. In old generation, it mainly stores memory objects with long lifecycles in applications, and permanentgeneration, which is mainly used to put JVM reflection objects, such as class objects and method objects.

In newgeneration blocks, the copying algorithm is generally used for garbage collection, which is fast. During each GC, the surviving objects are first copied from Eden to a certain sequence vorspace. When the sequence vor space is full, the remaining live objects are directly copied to oldgeneration. Therefore, after each GC, the Eden memory block is cleared. In the oldgeneration block, the Mark-compact algorithm is generally used for garbage collection, which is slower, but reduces memory requirements.

Garbage collection is divided into multiple levels. garbage collection in the old section will be collected when the level 0 is full. garbage collection in the Level 1 or above will only be collected in the new section, memory overflow usually occurs after garbage collection of the old or perm segments, and there is still no memory space to accommodate new Java objects.

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 recovery does not solve the problem of insufficient memory heap, the out of memory error will be 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. Displays the call to system. GC () can only be used to recommend that the JVM recycle junk objects in the memory, but it is not necessary to recycle them immediately. One is that it cannot solve the problem of insufficient memory resources, GC consumption is also increased.

When a URL is accessed, the memory application process is as follows:
A. JVM will try to initialize a memory area for the relevant Java object in Eden
B. When the Eden space is sufficient, the memory application is completed. Otherwise, go to the next step.
C. JVM tries to release all inactive objects in Eden (which belongs to 1 or more advanced garbage collection). If the Eden space is not enough to put new objects, try to put some active objects in Eden into the same vor Area
D. The primary vor area is used as the intermediate swap area of Eden and old. When the old area has sufficient space, objects in the primary vor area will be moved to the old area. Otherwise, objects in the primary vor area will be retained.
E. When there is not enough space in the old area, JVM will perform full garbage collection in the old area (Level 0)
F. after full garbage collection, if the primary VOR and old areas still cannot store some objects copied from Eden, the JVM cannot create a memory area for the new object in the Eden area, the error "out of memory" appears"

Ii. memory overflow type 
1. java. Lang. outofmemoryerror: permgen Space
JVM manages two types of memory, heap and non-heap. Heap is used by developers and 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 webapp 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
This memory overflow is one of the most common cases, mainly reflected in the old overflow, the cause may be: 1) set the memory parameter is too small (MS/MX, newsize/maxnewsize); 2) program problems. The default heapspace (-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.

3. java. Lang. outofmemoryerror: C heap Space
The system has no restrictions on C heap. Therefore, when a problem occurs in C heap, the memory occupied by Java processes will continue to grow until all available system memory is occupied.

4. Others
JVM has two GC threads. The first thread is responsible for revoking heap's young zone. When the heap is insufficient, the second thread traverses the heap and upgrades the young area to the older area. The size of the older area is equal to-xmx minus-xmn, and the value of-XMS cannot be set too large, because the second thread is forced to run will reduce JVM performance.
Why does GC occur frequently in some programs? There are the following reasons:
A. The program calls system. GC () or runtime. GC ().
B. Some middleware software calls its own GC method. In this case, you need to set parameters to disable these GC methods.
C. Java heap is too small. Generally, the default heap value is very small.
D. Frequently instantiate the image and release object. In this case, try to save and reuse objects, for example, using stringbuffer () and string ().
If you find that after each GC, the remaining heap space will be 50% of the total space, which indicates that your heap is healthy. Many server-side Java programs have a better 65% free space after each GC.

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
Note: increasing the heap size reduces the GC frequency, but increases the GC time. When GC is run, all user threads are paused, that is, Java applications do not work during GC.
Tip: heap size does not determine the memory usage of the process. The memory usage of a process must be greater than the value defined by-xmx, Because Java allocates memory for other tasks, such as the stack of each thread.
Tip: Each thread has its own stack. The stack size limits the number of threads. If the stack is too large, the memory may leak. -The XSS parameter determines the stack size, for example,-xss1024k. If the stack is too small, the stack may leak.

JVM uses-XX: permsize to set the non-heap memory initial value. The default value is 1/64 of the physical memory. The maximum non-heap memory size is set 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"

4. 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. For example
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.
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.

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.