Bli*_*lin 5 java out-of-memory
我有几个应用程序在指定的时间间隔内运行.为了监控OutOfMemoryError,我决定启用HeapDumpOnOutOfMemoryError,在此之前,我决定做一些研究.某些应用程序的最大堆大小为2GB,因此快速连续生成多个堆转储会占用所有磁盘空间.
我写了一个小脚本来检查它是如何工作的.
import java.util.LinkedList;
import java.util.List;
public class Test implements Runnable{
public static void main(String[] args) throws Exception {
new Thread(new Test()).start();
}
public void run() {
while (true) {
try{
List<Object> list = new LinkedList<Object>();
while (true){
list.add(new Object());
}
}
catch (Throwable e){
System.out.println(e);
}
try {
Thread.sleep(1000);
}
catch (InterruptedException ignored) {
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是结果
$ java -XX:+HeapDumpOnOutOfMemoryError -Xmx2M Test
java.lang.OutOfMemoryError: Java heap space
Dumping heap to java_pid25711.hprof ...
Heap dump file created [14694890 bytes in 0,101 secs]
java.lang.OutOfMemoryError: Java heap space
java.lang.OutOfMemoryError: Java heap space
Run Code Online (Sandbox Code Playgroud)
它可以像我希望的那样工作,但我想知道为什么.
看看openjdk6源代码,我发现了以下内容
void report_java_out_of_memory(const char* message) {
static jint out_of_memory_reported = 0;
// A number of threads may attempt to report OutOfMemoryError at around the
// same time. To avoid dumping the heap or executing the data collection
// commands multiple times we just do it once when the first threads reports
// the error.
if (Atomic::cmpxchg(1, &out_of_memory_reported, 0) == 0) {
// create heap dump before OnOutOfMemoryError commands are executed
if (HeapDumpOnOutOfMemoryError) {
tty->print_cr("java.lang.OutOfMemoryError: %s", message);
HeapDumper::dump_heap_from_oome();
}
if (OnOutOfMemoryError && OnOutOfMemoryError[0]) {
VMError err(message);
err.report_java_out_of_memory();
}
}
}
Run Code Online (Sandbox Code Playgroud)
第一个if语句如何工作?
编辑:似乎每次打印消息时都应该创建heapdump,但它不会发生.为什么会这样?
if 语句包含一个比较和交换原子操作,当且仅当交换是由正在运行的线程执行时,该操作才会返回 0。比较和交换(也称为比较和交换)的工作方式如下:
| 归档时间: |
|
| 查看次数: |
961 次 |
| 最近记录: |