美文网首页
JVM_JMM: StackOverFlow栈溢出

JVM_JMM: StackOverFlow栈溢出

作者: CalmHeart | 来源:发表于2019-07-06 23:36 被阅读0次
    • 使用递归演示,自己调用自己导致栈空间溢出,递归和回溯。
      源代码:MyTest3.java
    public class MyTest3 {
    
    
      private int length;
    
    
      public int getLength() {
        return length;
      }
    
    
      public void test() {
    
        this.length++;
    
        test();
    
      }
    
    
      public static void main(String[] args) {
    
        MyTest3 myTest3 = new MyTest3();
    
        try {
          myTest3.test();
        } catch (Throwable throwable) {
          System.out.println(myTest3.getLength());
          throwable.printStackTrace();
        }
      }
    
    }
    

    通过设置VMOption:-Xss100k 设置堆栈的大小是512k(最小是160k)
    运行结果:

    5460
    java.lang.StackOverflowError
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:17)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)
    at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:19)


    可以将参数调整的大一点,通过VisualVM进行监控程序的运行。
    通过线程的dump采集现场的情况:

    2019-07-06 23:20:52
    Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.162-b12 mixed mode):
    
    "RMI TCP Connection(2)-192.168.99.1" #16 daemon prio=5 os_prio=0 tid=0x000000001bf8f000 nid=0x41f8 runnable [0x000000001c4ee000]
       java.lang.Thread.State: RUNNABLE
            at java.net.SocketInputStream.socketRead0(Native Method)
            at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
            at java.net.SocketInputStream.read(SocketInputStream.java:171)
            at java.net.SocketInputStream.read(SocketInputStream.java:141)
            at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
            at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
            - locked <0x0000000782b42650> (a java.io.BufferedInputStream)
            at java.io.FilterInputStream.read(FilterInputStream.java:83)
            at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:550)
            at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
            at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
            at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$6/665453966.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
            at java.lang.Thread.run(Thread.java:748)
    
       Locked ownable synchronizers:
            - <0x0000000782592ea8> (a java.util.concurrent.ThreadPoolExecutor$Worker)
    
    "JMX server connection timeout 15" #15 daemon prio=5 os_prio=0 tid=0x000000001bf73800 nid=0x5328 in Object.wait() [0x000000001ba7f000]
       java.lang.Thread.State: TIMED_WAITING (on object monitor)
            at java.lang.Object.wait(Native Method)
            - waiting on <0x00000007827ebec0> (a [I)
            at com.sun.jmx.remote.internal.ServerCommunicatorAdmin$Timeout.run(ServerCommunicatorAdmin.java:168)
            - locked <0x00000007827ebec0> (a [I)
            at java.lang.Thread.run(Thread.java:748)
    
       Locked ownable synchronizers:
            - None
    
    "RMI Scheduler(0)" #14 daemon prio=5 os_prio=0 tid=0x000000001bf73000 nid=0x1174 waiting on condition [0x000000001b80e000]
       java.lang.Thread.State: TIMED_WAITING (parking)
            at sun.misc.Unsafe.park(Native Method)
            - parking to wait for  <0x0000000782526ea0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
            at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
            at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
            at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
            at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
            at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
            at java.lang.Thread.run(Thread.java:748)
    
       Locked ownable synchronizers:
            - None
    
    "RMI TCP Connection(1)-192.168.99.1" #13 daemon prio=5 os_prio=0 tid=0x000000001bf65800 nid=0x210 runnable [0x000000001b9fe000]
       java.lang.Thread.State: RUNNABLE
            at java.net.SocketInputStream.socketRead0(Native Method)
            at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
            at java.net.SocketInputStream.read(SocketInputStream.java:171)
            at java.net.SocketInputStream.read(SocketInputStream.java:141)
            at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
            at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
            - locked <0x0000000782779ec8> (a java.io.BufferedInputStream)
            at java.io.FilterInputStream.read(FilterInputStream.java:83)
            at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:550)
            at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
            at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
            at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$6/665453966.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
            at java.lang.Thread.run(Thread.java:748)
    
       Locked ownable synchronizers:
            - <0x000000078258f2c8> (a java.util.concurrent.ThreadPoolExecutor$Worker)
    
    "RMI TCP Accept-0" #12 daemon prio=5 os_prio=0 tid=0x000000001bef7000 nid=0x3f30 runnable [0x000000001b17e000]
       java.lang.Thread.State: RUNNABLE
            at java.net.DualStackPlainSocketImpl.accept0(Native Method)
            at java.net.DualStackPlainSocketImpl.socketAccept(DualStackPlainSocketImpl.java:131)
            at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)
            at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:199)
            - locked <0x0000000782534b80> (a java.net.SocksSocketImpl)
            at java.net.ServerSocket.implAccept(ServerSocket.java:545)
            at java.net.ServerSocket.accept(ServerSocket.java:513)
            at sun.management.jmxremote.LocalRMIServerSocketFactory$1.accept(LocalRMIServerSocketFactory.java:52)
            at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.executeAcceptLoop(TCPTransport.java:400)
            at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.run(TCPTransport.java:372)
            at java.lang.Thread.run(Thread.java:748)
    
       Locked ownable synchronizers:
            - None
    
    "Service Thread" #10 daemon prio=9 os_prio=0 tid=0x000000001afeb000 nid=0x254c runnable [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
       Locked ownable synchronizers:
            - None
    
    "C1 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001af4a000 nid=0x3e64 waiting on condition [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
       Locked ownable synchronizers:
            - None
    
    "C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001af47000 nid=0x56d0 waiting on condition [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
       Locked ownable synchronizers:
            - None
    
    "C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001af46000 nid=0x4a5c waiting on condition [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
       Locked ownable synchronizers:
            - None
    
    "Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001af0b000 nid=0x4b6c runnable [0x000000001b34e000]
       java.lang.Thread.State: RUNNABLE
            at java.net.SocketInputStream.socketRead0(Native Method)
            at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
            at java.net.SocketInputStream.read(SocketInputStream.java:171)
            at java.net.SocketInputStream.read(SocketInputStream.java:141)
            at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
            at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
            at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
            - locked <0x0000000781d1d408> (a java.io.InputStreamReader)
            at java.io.InputStreamReader.read(InputStreamReader.java:184)
            at java.io.BufferedReader.fill(BufferedReader.java:161)
            at java.io.BufferedReader.readLine(BufferedReader.java:324)
            - locked <0x0000000781d1d408> (a java.io.InputStreamReader)
            at java.io.BufferedReader.readLine(BufferedReader.java:389)
            at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)
    
       Locked ownable synchronizers:
            - None
    
    "Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001acdb000 nid=0x5330 waiting on condition [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
       Locked ownable synchronizers:
            - None
    
    "Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001acda800 nid=0x4e54 runnable [0x0000000000000000]
       java.lang.Thread.State: RUNNABLE
    
       Locked ownable synchronizers:
            - None
    
    "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x0000000019b88800 nid=0x560 in Object.wait() [0x000000001acce000]
       java.lang.Thread.State: WAITING (on object monitor)
            at java.lang.Object.wait(Native Method)
            - waiting on <0x0000000781a08ec0> (a java.lang.ref.ReferenceQueue$Lock)
            at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
            - locked <0x0000000781a08ec0> (a java.lang.ref.ReferenceQueue$Lock)
            at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
            at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:212)
    
       Locked ownable synchronizers:
            - None
    
    "Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000019b47000 nid=0x1248 in Object.wait() [0x000000001ab9f000]
       java.lang.Thread.State: WAITING (on object monitor)
            at java.lang.Object.wait(Native Method)
            - waiting on <0x0000000781a06b68> (a java.lang.ref.Reference$Lock)
            at java.lang.Object.wait(Object.java:502)
            at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
            - locked <0x0000000781a06b68> (a java.lang.ref.Reference$Lock)
            at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
    
       Locked ownable synchronizers:
            - None
    
    "main" #1 prio=5 os_prio=0 tid=0x00000000024bc000 nid=0x521c waiting on condition [0x00000000026cb000]
       java.lang.Thread.State: TIMED_WAITING (sleeping)
            at java.lang.Thread.sleep(Native Method)
            at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:20)
            at com.compass.spring_lecture.memory.MyTest3.test(MyTest3.java:24)
    Locked ownable synchronizers:
            - None
    
    "VM Thread" os_prio=2 tid=0x0000000019b3f800 nid=0x2104 runnable 
    
    "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000024d0800 nid=0x1ae0 runnable 
    
    "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000024d2000 nid=0x100c runnable 
    
    "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00000000024d3800 nid=0x4164 runnable 
    
    "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00000000024d5000 nid=0x3724 runnable 
    
    "VM Periodic Task Thread" os_prio=2 tid=0x000000001b030000 nid=0x3944 waiting on condition 
    
    JNI global references: 336
    

    也可以通过使用jconsole来监视:
    C:\Users\Administrator>jconsole

    image.png

    相关文章

      网友评论

          本文标题:JVM_JMM: StackOverFlow栈溢出

          本文链接:https://www.haomeiwen.com/subject/tqxvhctx.html