英文:
Unexpected error occurred in scheduled task. java.lang.OutOfMemoryError: Java heap space
问题
I am using java websoket client to create a websocket connnect in java 8, this is my 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("${com.dabai.robot.ws-url}")
private String websocketUrl;
@Autowired
private RobotService robotService;
public WebsocketClientEndpoint robotNewConnect(Long roomTypeId, String token, String userMark) {
WebsocketClientEndpoint clientEndPoint = null;
String websocketConnUrl = websocketUrl + "?token=" + token + "&roomTypeId=" + roomTypeId + "&robotFlag=1";
try {
String appMark = SessionUtil.getThreadLocal("appMark");
clientEndPoint = new WebsocketClientEndpoint(new URI(websocketConnUrl));
clientEndPoint.userSession.getUserProperties().put("userIdentity", userMark + "-" + appMark + "-" + roomTypeId);
clientEndPoint.addMessageHandler(message -> {
log.info("addMessageHandler:", message);
});
} catch (Exception e) {
log.error("Websocket", e);
}
return clientEndPoint;
}
}
recently this code throws Unexpected error occurred in scheduled task. java.lang.OutOfMemoryError: Java heap space
makes me confusing. this is the full error stack output:
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.<init>(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.<init>(WsFrameBase.java:96) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
at org.apache.tomcat.websocket.WsFrameClient.<init>(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.<init>(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]
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:
ENTRYPOINT exec java -Dfile.encoding=UTF-8 -Xmx512M -Xms128M \
-Xdebug -Xrunjdwp:transport=dt_socket,suspend=n,server=y,address=5009 \
-
<details>
<summary>英文:</summary>
I am using java websoket client to create a websocket connnect in java 8, this is my 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("${com.dabai.robot.ws-url}")
private String websocketUrl;
@Autowired
private RobotService robotService;
public WebsocketClientEndpoint robotNewConnect(Long roomTypeId, String token, String userMark) {
WebsocketClientEndpoint clientEndPoint = null;
String websocketConnUrl = websocketUrl + "?token=" + token + "&roomTypeId=" + roomTypeId + "&robotFlag=1";
try {
String appMark = SessionUtil.getThreadLocal("appMark");
clientEndPoint = new WebsocketClientEndpoint(new URI(websocketConnUrl));
clientEndPoint.userSession.getUserProperties().put("userIdentity", userMark + "-" + appMark + "-" + roomTypeId);
clientEndPoint.addMessageHandler(message -> {
log.info("addMessageHandler:", message);
});
} catch (Exception e) {
log.error("Websocket", e);
}
return clientEndPoint;
}
}
recently this code throws ` Unexpected error occurred in scheduled task.
java.lang.OutOfMemoryError: Java heap space` makes me confusing. this is the full error stack output:
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.<init>(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.<init>(WsFrameBase.java:96) ~[tomcat-embed-websocket-9.0.30.jar!/:9.0.30]
at org.apache.tomcat.websocket.WsFrameClient.<init>(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.<init>(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]
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:
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
</details>
# 答案1
**得分**: 0
使用命令行选项```-Xmx```运行Java,该选项设置了堆的最大大小。
[详细信息请参见此处][1]
[1]: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html#nonstandard
<details>
<summary>英文:</summary>
Run Java with the command-line option ```-Xmx``` , which sets the maximum size of the heap.
[See here for details][1]
[1]: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html#nonstandard
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论