gpt4 book ai didi

java - 写Java代码让java虚拟机崩溃

转载 作者:搜寻专家 更新时间:2023-10-31 08:05:02 25 4
gpt4 key购买 nike

在我过去的一次采访中,有人让我写一段代码让 JVM 崩溃。我说的是 System.exit()。这样对吗?有没有更好的答案?

说明:我可以在开发和部署期间包含我的代码段。并不是 JVM 已经在运行,我必须编写一个黑客代码来使另一个 JVM 崩溃。

最佳答案

您可以使用 Unsafe 类,您可能会猜到它是不安全的。

public static void main(String... args) throws Exception {
getUnsafe().getByte(0);
}

private static Unsafe getUnsafe() throws NoSuchFieldException, IllegalAccessException {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(null);
}

打印

#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007ff1c2f23368, pid=2630, tid=140676351506176
#
# JRE version: 7.0-b147
# Java VM: Java HotSpot(TM) 64-Bit Server VM (21.0-b17 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# V [libjvm.so+0x82c368] Unsafe_GetNativeByte+0xa8
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /nfs/peter/IdeaProjects/scratch/hs_err_pid2630.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
#

我用它来测试何时对文件进行了更改。我进行了更改并使 JVM 崩溃,以确保其他内容不会刷新或稍后出现。然后我检查我看到了我预期的更新。

关于java - 写Java代码让java虚拟机崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7771068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com