How do you crash a JVM?

前端 未结 27 1138
故里飘歌
故里飘歌 2020-11-28 00:22

I was reading a book on programming skills wherein the author asks the interviewee, \"How do you crash a JVM?\" I thought that you could do so by writing an infinite for-loo

相关标签:
27条回答
  • 2020-11-28 01:23

    on winxpsp2 w/wmp10 jre6.0_7

    Desktop.open(uriToAviOrMpgFile)

    This causes a spawned thread to throw an uncaught Throwable and crashes hotspot

    YMMV

    0 讨论(0)
  • 2020-11-28 01:25

    shortest possible way :)

    public class Crash
    {
        public static void main(String[] args)
        {
            main(args);
        }
    }
    
    0 讨论(0)
  • 2020-11-28 01:25

    If you define a crash as an process abort because of a unhandled situation (i.e. no Java Exception or Error), then this can not be done from within Java (unless you have permission to use the sun.misc.Unsafe class). This the whole point of managed code.

    Typical crashes in native code happen by de-referencing pointers to wrong memory areas (null address or missaligned). Another source could be illegal machine instructions (opcodes) or unhandled signals from library or kernel calls. Both can be triggered if the JVM or the system libraries have bugs.

    For example JITed (generated) code, native methods or system calls (graphics driver) can have problems leading to real crashes (it was quite common to get a crash when you used ZIP functions and they ran out of memory). In those cases the crash handler of the JVM kicks in and dumps the state. It could also generate a OS core file (Dr. Watson on Windows and core dump on *nix).

    On Linux/Unix you can easyly make a JVM crash by sending it a Signal to the running process. Note: you should not use SIGSEGV for this, since Hotspot catches this signal and re-throws it as a NullPointerException in most places. So it is better to send a SIGBUS for example.

    0 讨论(0)
提交回复
热议问题