title: Unexpected error occurred in scheduled task. java.lang.OutOfMemoryError: Java heap space
body: <p>I am using java websoket client to create a websocket connnect in java 8, this is my code:</p>
  <pre><code>import com.sportswin.soa.robot.common.SessionUtil;
  import com.sportswin.soa.robot.model.entity.Robot;
  import com.sportswin.soa.robot.model.enumn.RobotStatus;
  import com.sportswin.soa.robot.service.impl.RobotService;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.stereotype.Component;
  
  import java.net.InetAddress;
  import java.net.URI;
  
  /**
   * @author dolphin
   */
  @Slf4j
  @Component
  public class WebsocketClientConnector {
  
      @Value(&quot;${com.dabai.robot.ws-url}&quot;)
      private String websocketUrl;
  
      @Autowired
      private RobotService robotService;
  
      public WebsocketClientEndpoint robotNewConnect(Long roomTypeId, String token, String userMark) {
          WebsocketClientEndpoint clientEndPoint = null;
          String websocketConnUrl = websocketUrl + &quot;?token=&quot; + token + &quot;&amp;roomTypeId=&quot; + roomTypeId + &quot;&amp;robotFlag=1&quot;;
          try {
              String appMark = SessionUtil.getThreadLocal(&quot;appMark&quot;);
              clientEndPoint = new WebsocketClientEndpoint(new URI(websocketConnUrl));
              clientEndPoint.userSession.getUserProperties().put(&quot;userIdentity&quot;, userMark + &quot;-&quot; + appMark + &quot;-&quot; + roomTypeId);
              clientEndPoint.addMessageHandler(message -&gt; {
                  log.info(&quot;addMessageHandler:&quot;, message);
              });
          } catch (Exception e) {
              log.error(&quot;Websocket&quot;, e);
          }
          return clientEndPoint;
      }
  }
  </code></pre>
  <p>recently this code throws <code> Unexpected error occurred in scheduled task. java.lang.OutOfMemoryError: Java heap space</code> makes  me confusing. this is the full error stack output:</p>
  <pre><code>2020-08-12 19:56:39.186 ERROR 1 --- [ool-10-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task.
  java.lang.OutOfMemoryError: Java heap space
    at java.nio.HeapCharBuffer.&lt;init&gt;(HeapCharBuffer.java:57) ~[na:1.8.0_212]
    at java.nio.CharBuffer.allocate(CharBuffer.java:335) ~[na:1.8.0_212]
    at org.apache.tomcat.websocket.WsFrameBase.&lt;init&gt;(WsFrameBase.java:96) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
    at org.apache.tomcat.websocket.WsFrameClient.&lt;init&gt;(WsFrameClient.java:43) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
    at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServerRecursive(WsWebSocketContainer.java:503) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
    at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:197) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
    at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:154) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
    at com.sportswin.soa.robot.websocket.client.WebsocketClientEndpoint.&lt;init&gt;(WebsocketClientEndpoint.java:53) ~[classes!/:na]
    at com.sportswin.soa.robot.websocket.client.WebsocketClientConnector.robotNewConnect(WebsocketClientConnector.java:46) ~[classes!/:na]
    at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.createWebsocketConnection(PlayEntryOperate.java:408) ~[classes!/:na]
    at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.createWebsocketHandle(PlayEntryOperate.java:387) ~[classes!/:na]
    at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.allowRobotSelfPlay(PlayEntryOperate.java:262) ~[classes!/:na]
    at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.lambda$matchByRoomType$2(PlayEntryOperate.java:195) ~[classes!/:na]
    at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate$$Lambda$923/1467190450.accept(Unknown Source) ~[na:na]
    at java.util.HashMap.forEach(HashMap.java:1289) ~[na:1.8.0_212]
    at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.matchByRoomType(PlayEntryOperate.java:181) ~[classes!/:na]
    at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.allRoomRobotBalance(PlayEntryOperate.java:170) ~[classes!/:na]
    at com.sportswin.soa.robot.jobhandler.robot.envelop.PlayEntryOperate.playRoomEntry(PlayEntryOperate.java:82) ~[classes!/:na]
    at com.sportswin.soa.robot.jobhandler.robot.envelop.EnvelopRobotJobHandler.matchTenant(EnvelopRobotJobHandler.java:62) ~[classes!/:na]
    at com.sportswin.soa.robot.jobhandler.robot.envelop.EnvelopRobotJobHandler.jobExecutor(EnvelopRobotJobHandler.java:48) ~[classes!/:na]
    at com.sportswin.soa.robot.jobhandler.robot.envelop.EnvelopRobotJobHandler.runJob(EnvelopRobotJobHandler.java:40) ~[classes!/:na]
    at sun.reflect.GeneratedMethodAccessor395.invoke(Unknown Source) ~[na:na]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_212]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_212]
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.1.13.RELEASE.jar!/:5.1.13.RELEASE]
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.1.13.RELEASE.jar!/:5.1.13.RELEASE]
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) ~[spring-context-5.1.13.RELEASE.jar!/:5.1.13.RELEASE]
  </code></pre>
  <p>I am searching from internet but seems no one encount this error. what should I do to fix? by the way, this is the startup script I am using:</p>
  <pre><code>ENTRYPOINT exec java -Dfile.encoding=UTF-8 -Xmx512M -Xms128M \
                  -Xdebug -Xrunjdwp:transport=dt_socket,suspend=n,server=y,address=5009 \
                  -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/data/dump \
                  -javaagent:/opt/skywalking/agent/skywalking-agent.jar \
                  -Dskywalking.agent.service_name=soa-robot-service \
                  -Dskywalking.collector.backend_service=oap:11800 \
                  -jar /root/soa-robot-service-1.0.0-SNAPSHOT.jar
  </code></pre>