java stackoverflowerror thrown in infinite loop

前端 未结 3 1731
挽巷
挽巷 2021-01-26 09:31

I have the following function that starts a jsvc daemon for receiving UDP messages:

 @Override
public void start() throws Exception {
    byte[] buf = new byte[1         


        
3条回答
  •  迷失自我
    2021-01-26 09:52

        public void run() {             
            while(!stopped){
    
                byte[] rcvMsg = incomingBinaryMessage;
    
                MessageCreator tmc = new MessageCreator();
                Message message = null;
                try {
                    message = tmc.createMessage(rcvMsg);
                System.out.println(message);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
    

    This code does not appear to do any I/O. incomingBinaryMessage is not a method invocation, it is an object reference to an existing byte[].

    The loop runs repeatedly creating the same message over and over.

    Normally GC should keep up with you since you're discarding the messages and MessageCreator instance on every loop. However, the one piece of code you haven't shown, the constructor for Message could be saving a reference to the messages (i.e. adding them to a map?) and preventing them from being GC'ed.

提交回复
热议问题