白衣非少年
通常在普通的Java程序中,您没有这个问题。但是,是的,如果您使用类似的东西可能会导致分段错误。但是,这就是为什么被称为.通常,您不必使用它,因此代码中就不会有此问题。sun.misc.UnsafeUnsafeUnsafe有关详细信息:sun.misc.Unsafe 记录在哪里?基于带有 https://github.com/eclipse/openj9/issues/4153 分段错误报告的错误sun.misc.Unsafe下面是一个如何测试的示例:import java.lang.reflect.Field;import sun.misc.Unsafe;public class MainClass {public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); theUnsafe.setAccessible(true); Unsafe unsafe = (Unsafe) theUnsafe.get(null); long ten = 10; byte size = 1; long mem = unsafe.allocateMemory(size); //Put here the wrong address!!! unsafe.putAddress(1, ten); //With this will work: //unsafe.putAddress(mem, ten); long readValue = unsafe.getAddress(mem); System.out.println("result: " + readValue);}}当我在Ubuntu 18.04上执行时,我得到这个输出:A fatal error has been detected by the Java Runtime Environment:SIGSEGV (0xb) at pc=0x00007f05bdf04d27, pid=4145, tid=4147JRE version: OpenJDK Runtime Environment (10.0.2+13) (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4)在Windows上,我认为它将是具有致命错误描述的类似输出。祝大家好运!