How do you crash a JVM?

前端 未结 27 1461
故里飘歌
故里飘歌 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:15

    If you create a thread process that infinitely spawns more threads (which spawn more threads, which...) you'll eventually cause a stack overflow error in the JVM itself.

    public class Crash {
        public static void main(String[] args) {
    
            Runnable[] arr = new Runnable[1];
            arr[0] = () -> {
    
                while (true) {
                    new Thread(arr[0]).start();
                }
            };
    
            arr[0].run();
        }
    }
    

    This gave me the output (after 5 minutes, watch your ram)

    An unrecoverable stack overflow has occurred.
    #
    # A fatal error has been detected by the Java Runtime Environment:
    #
    #  EXCEPTION_STACK_OVERFLOW (0xc00000fd) at pc=0x0000000070e53ed7, pid=12840, tid=0x0000000000101078
    #
    # JRE version: Java(TM) SE Runtime Environment (8.0_144-b01) (build 1.8.0_144-b01)
    # Java VM: Java HotSpot(TM) 64-Bit Server VM (25.144-b01 mixed mode windows-amd64 compressed oops)
    # Problematic frame:
    # 
    

提交回复
热议问题