Java Memory leak overflow

Source: Internet
Author: User
Tags server memory xms jprofiler

Points

    • Memory leak refers to the program in the middle of the dynamic allocation of memory, but at the end of the program does not release this part of memory, resulting in the case that part of the memory is not available, restart the computer can be resolved, but there may be a memory leak again, memory leaks and hardware is not related, it is caused by software design flaws.
    • Memory leaks can be divided into 4 categories:

1) frequent memory leaks. The code that occurs in memory leaks is executed multiple times, causing a memory leak each time it is executed.

2) Accidental memory leaks. Code that occurs with a memory leak occurs only under certain circumstances or during operation. The occurrence and the incidental sex are opposite. For a given environment, the occasional may become a frequent occurrence. So test environments and test methods are critical to detecting memory leaks.

3) disposable memory leaks. The code that occurs with a memory leak is only executed once, or because of an algorithmic flaw, there is always a piece of memory that leaks. For example, allocating memory in the class's constructor does not release the memory in the destructor, so a memory leak occurs only once.

4) An implicit memory leak. The program keeps allocating memory while it is running, but it does not release memory until the end. Strictly speaking, there is no memory leak, because the final program frees up all the requested memory. But for a server program that needs to run for days, weeks, or months, not releasing memory in time can also result in the eventual exhaustion of all of the system's memory. So, we call this kind of memory leak as an implicit memory leak.

    • Memory Overflow Type:

1) Java.lang.OutOfMemoryError:PermGen Space

The full name of PermGen space is Permanent Generation space, which refers to the permanent storage area of memory. This memory is primarily stored by the JVM class and meta information, class is loader when it is placed in the Permgenspace, and the storage class instance (Instance) of the heap area, GC will not be in the main program run time to PermGen Space for cleanup.

The JVM sets the non-heap memory initial value by Xx:permsize, which defaults to 1/64 of the physical memory;

The JVM sets the maximum non-heap memory size by xx:maxpermsize, which is 1/4 of the physical memory by default.

Common scenarios for this error:

A) There are many Class,web servers in the application when the JSP is precompile.

b) A large number of third-party jars are used under WebApp, which exceeds the default size of the JVM (4M).

2) Java.lang.OutOfMemoryError:Java Heap Space

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%.

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 maximum value of the JVM memory is very much related to the operating system. 32-bit processor Although the controllable memory space has 4GB, but the specific operating system will give a limit, this limit is generally 2GB-3GB (generally speaking under the Windows system for the 1.5g-2g,linux system is 2g-3g), and more than 64bit processors 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.

Common scenarios for this error:

A) When uploading files to the Web.

b) Open large files or fetch too much data from the database at one time.

Related issues

1. Is there a memory leak in the Q:java?
There is also a memory leak in the A:java. This is caused when the allocated object is up to date and is useless (no null for the reference to the deprecated data Memory unit).

Such as:

Java code

    1. Vector v=new vector (10);
    2. for (int i=1;i<100; i) {
    3. Object o=New object ();
    4. V.add (o);
    5. o=null;
    6. }
    7. At this point, all object objects are not freed because the variable v refers to these objects.
    8. After the object is added to the vector, it must also be removed from the vector, and the simplest way to release it is to set the vector object to null.

2. Q: What are the similarities and differences between memory leaks and overflows?

A: The same: can cause problems with the application running, degraded performance, or hangs.

Vary

1) memory leaks are one of the causes of memory overflow, and memory leaks accumulate to cause memory overflow.

2) memory leaks can be avoided by perfecting the code, and memory overflows can be adjusted to reduce the frequency of occurrence, but cannot be completely avoided.

3. How do I detect a memory leak?

A: Some performance monitoring and analysis tools are available, such as Jprofiler, Optimizeit Profiler.

4. Q: How to avoid memory leaks, overflow?
a:1) Release references to useless objects as early as possible.

A good idea is to use a temporary variable to have the reference variable automatically set to null after exiting the active domain, suggesting that the garbage collector collects the object to prevent a memory leak.

2) When the program does string processing, try to avoid using string, instead use StringBuffer.

Because each string object occupies a single area of memory, such as:

Java code

    1. String str = "AAA";
    2. String str2 = "BBB";
    3. String STR3 = str + str2;
    4. If STR is not called after the execution of this str2, then they will wait for GC recovery in memory;
    5. If there are too many similar cases in the program, memory error will occur;

3) Use as few static variables as possible.

Because static variables are global, the GC is not recycled.

4) Avoid concentrating on creating objects, especially large objects, and use flow operations if possible.

The JVM will suddenly need a lot of memory, which will trigger the GC to optimize the system memory environment, one case is as follows:

Java code

    1. Using Jspsmartupload as a file upload, java.outofmemoryerror errors often occur during the operation,
    2. Problem found after check: code in component
    3. M_totalbytes = M_request.getcontentlength ();
    4. M_binarray = new byte[m_totalbytes];
    5. TotalBytes This variable gets a large number, causing the array to allocate a lot of memory space, and the array will not be released in time.
    6. The solution can only be changed in a more appropriate way, at least not by triggering a outofmemoryerror solution.
    7. Reference: http://bbs.xml.org.cn/blog/more.asp?name=hongrui&id=3747

5) Try to use the object pooling technique to improve the system performance.

Long-life objects that have short life-cycle objects are prone to memory leaks, 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 having to discard them each time you new.

7) Optimize the configuration.

5. Q: A memory overflow solution?
A: First, from the code level to optimize and improve, to avoid the situation as far as possible;

The second is to adjust the optimized server configuration:

1) Set-XMS,-xmx equal;

2) Set newsize, Maxnewsize equal;

3) Set heap size, PermGen space:

Example of TOMCAT configuration: modifying%tomcat_home%/bin/catalina.bat or catalina.sh

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

CMD code

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

How to resolve memory overflow

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 permanentgeneration 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 into the PermGen Space, unlike the heap area where the class instance (Instance) is stored, GC (garbagecollection) does not clean up permgen space during the main program run time, so if you have a class in your application, Permgenspace 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: (after I have verified, since the use of this configuration, there is no more tomcat dead situation)

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.

Role of 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 garbage objects in memory, but it does not have to be recycled immediately.

One is not able to solve the memory resource depletion situation, but also increase the consumption of GC.

Second, the JVM memory area composition

Simple to say heap and Stack in Java

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

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

2. Heap memory is used to hold 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 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 relatively slow;

The advantage of the stack is that the access speed is faster than the heap, and the disadvantage is that the data size in the stack and the lifetime must be deterministic 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

Java Stack Storage

Stack adjustment: Parameters have +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.

When the default free heap memory is less than 40%, the JVM increases the heap until the maximum limit of-xmx, and when the free heap memory is greater than 70%, the JVM reduces the heap until the minimum limit of-XMS. So the server generally sets-xms,-xmx equal to avoid resizing the heap after each GC.

Tip: Assuming that the physical memory is infinitely large, the maximum value of the JVM memory is very much related to the operating system.

In short, the 32-bit processor, although the controllable memory space has 4GB, but the specific operating system will give a limit,

This limit is generally 2GB-3GB (generally under Windows System is 2g-3g under the 1.5g-2g,linux system), and processors over 64bit are not limited

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

Tip: Set newsize, Maxnewsize equal, "new" should not be more than half the size of "old", because the old area if not enough to trigger "main" GC frequently, greatly reducing the performance

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"

Iv. use of performance check tools

To locate a memory leak:

The Jprofiler tool is primarily used to examine and track the performance of the system (limited to Java development). Jprofiler can monitor the operation of the JVM and its performance by monitoring the memory usage of the system and monitoring the garbage collection, thread health and so on at all times.

1. Application server memory Long-term unreasonable occupation, memory is often in high occupancy, it is difficult to recover low;

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

3. The application server often does full GC (garbagecollection), and the time is very long, about 30-40 seconds, the application server when doing full GC is not responding to the client's transaction request, very affect system performance.

Because the development environment and the product environment will be different, causing the problem to occur sometimes in the production environment, you can usually use the tool to track the memory usage of the system, in some cases, perhaps at some point in time is indeed a large amount of memory caused out of memories, should continue to follow the next to see if there will be a decline ,

If it stays high, it's definitely a memory leak because of the program's cause.

Five, 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.

This is a case for everyone to alert.

Using Jspsmartupload as a file upload, java.outofmemoryerror errors often occur during the operation,

Problem found after check: code in component

M_totalbytes = M_request.getcontentlength ();

M_binarray = new Byte[m_totalbytes];

The problem is that the totalbytes variable gets a large number, which causes the array to allocate a lot of memory space, and the array cannot be released in time. The solution can only be changed in a more appropriate way, at least not by triggering a outofmemoryerror solution. Reference: http://bbs.xml.org.cn/blog/more.asp?name=hongrui&id=3747

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 situation of out ofmemory Error, it is likely to calculate the maximum amount of data is how much, and set the required minimum and maximum memory space value.

Java Memory leak overflow

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.