Java oom problem Troubleshooting

Source: Internet
Author: User

When doing server-side development, often encountered services due to the memory of the hanging out of the situation, this situation is generally difficult to anticipate, and more difficult to reproduce, for this problem, generally can be recorded memory overflow when the heap of information to troubleshoot.


1, you can first view the server run log and the log of the project record, to catch memory overflow exception.


2, if the program hangs out, but did not find any log records of this operation. Now look at the/var/log/messages file. The messages log is the core system log file. It contains the boot message when the system starts, and other status messages when the system is running. The following information will appear in the messages:

Figure 1

This is because both the server and the project log can only record memory overflows that occur within the JVM, meaning that the size of the heap (heap) exceeds the size of the JVM setting. However, if the JVM sets a heap size that exceeds the amount of memory allowed by the operating system, then the operating system kills the process directly, a situation in which the JVM cannot log this operation.

As you can see from Figure 1, the operating system kills the highest rated process directly because of the high memory usage, and this is the Java process. Linux has a Oom rating for each process, and this score is in the/proc/pid/oom_score file. For example/proc/8398/oom_score, if you do not want to kill the process, change the Oom_adj content to-17.


3, in this case the first need to adjust the JVM heap size, in the run parameters set

-xms20m-xmx20m

The heap is smaller so that the JVM's oom takes precedence over the oom of the operating system. Then set the running parameters and output the Heapdump file when the oom occurs.

-xx:+heapdumponoutofmemoryerror

-xx:heapdumppath=/home/admin/logs/java.hprof


4, a java.hprof file is generated when a Oom exception is caught.

Examples of programs that produce oom:

public class Outofmemeorysample {
public static Voidmain (string[] args) {
Headoutofmemory ();
}
static void Headoutofmemory () {
Long count= 0;
try {
list<object> objects = newarraylist<object> ();
while (true) {
count++;
Objects.add (New Object ());
}
catch (Throwable ex) {
System.out.println (count);
Ex.printstacktrace ();
}
}
}

5, install the Mat tool in Eclipse. Details: http://jingyan.baidu.com/article/cb5d61053562ed005c2fe022.html

Import the output java.hprof file into Mat, as shown in Figure 2, to see the heap state at the time of Oom.

Figure 2

Click Histogram to see the list of objects in all heap, and the number of objects per class, as shown in Figure 3.

Figure 3

Right-click the object and select list Object->with incoming reference to see how the object is referenced by those objects, to find out why the object has not been reclaimed, as shown in Figure 4.

Figure 4

View suspicious classes through the Mat tool. After some of the code is executed and after the execution of the dump contrast, you can find the object has not been released.

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.