JVM 不会在 OnOutOfMemoryError 上被杀死
JVM is not get killed on OnOutOfMemoryError
我正在使用 JDK 1.8.0_u66
,-XX:+CrashOnOutOfMemoryError
选项尚不可用。所以我最终设置 -XX:OnOutOfMemoryError="kill -9 %p"
好的,我写了下面这个简单的应用程序:
public static void main(String[] args) throws Exception{
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
while (true) {
new Thread(() -> {
lock.lock();
try {
while (true) {
try{
condition.await();
} catch (Exception e){}
}
} finally {
lock.unlock();
}
}).start();
}
}
应用程序失败并出现错误
Exception in thread "main" java.lang.OutOfMemoryError:
unable to create new native thread
一瞬间,但它并没有像我预期的那样被杀死,因为我设置了选项-XX:OnOutOfMemoryError="kill -9 %p"
。
我想杀死 JVM 是在一个单独的 Thread
中完成的,但不可能创建一个,因为已经达到了限制。所以应用程序静静地保持在不一致的状态下工作。
您很可能受到 JDK-8155004 的影响,当 OutOfMemoryError
是由线程创建引起时,这会导致 -XX:OnOutOfMemoryError
和相关选项无法 运行。
当 OutOfMemoryError
是由堆内存不足引起时,您当前的方法很有效。您可以使用以下代码和 -XX:OnOutOfMemoryError="kill -9 %p"
选项对此进行测试。它退出 JVM 1.8.0_191
(最接近你的版本):
public static void main() {
Thread t = new Thread(() -> {
try {
TimeUnit.HOURS.sleep(30);
} catch (InterruptedException e) {}
});
t.start(); // to prevent natural JVM exit when main dies
ArrayList<Long[]> retain = new ArrayList<>();
while (true) {
Long[] arr = new Long[Integer.MAX_VALUE];
retain.add(arr);
}
}
我正在使用 JDK 1.8.0_u66
,-XX:+CrashOnOutOfMemoryError
选项尚不可用。所以我最终设置 -XX:OnOutOfMemoryError="kill -9 %p"
好的,我写了下面这个简单的应用程序:
public static void main(String[] args) throws Exception{
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
while (true) {
new Thread(() -> {
lock.lock();
try {
while (true) {
try{
condition.await();
} catch (Exception e){}
}
} finally {
lock.unlock();
}
}).start();
}
}
应用程序失败并出现错误
Exception in thread "main" java.lang.OutOfMemoryError:
unable to create new native thread
一瞬间,但它并没有像我预期的那样被杀死,因为我设置了选项-XX:OnOutOfMemoryError="kill -9 %p"
。
我想杀死 JVM 是在一个单独的 Thread
中完成的,但不可能创建一个,因为已经达到了限制。所以应用程序静静地保持在不一致的状态下工作。
您很可能受到 JDK-8155004 的影响,当 OutOfMemoryError
是由线程创建引起时,这会导致 -XX:OnOutOfMemoryError
和相关选项无法 运行。
当 OutOfMemoryError
是由堆内存不足引起时,您当前的方法很有效。您可以使用以下代码和 -XX:OnOutOfMemoryError="kill -9 %p"
选项对此进行测试。它退出 JVM 1.8.0_191
(最接近你的版本):
public static void main() {
Thread t = new Thread(() -> {
try {
TimeUnit.HOURS.sleep(30);
} catch (InterruptedException e) {}
});
t.start(); // to prevent natural JVM exit when main dies
ArrayList<Long[]> retain = new ArrayList<>();
while (true) {
Long[] arr = new Long[Integer.MAX_VALUE];
retain.add(arr);
}
}