java - OutOfMemoryError : Direct buffer memory when using websockets in WildFly

标签 java websocket out-of-memory wildfly undertow

在我们的 WildFly 18 服务器上一段时间后,在生产中,我们遇到了这个错误:

[org.xnio.listener] (default I/O-1) XNIO001007: A channel event listener threw an exception: 
java.lang.OutOfMemoryError: Direct buffer memory
    at java.base/java.nio.Bits.reserveMemory(Bits.java:175)
    at java.base/java.nio.DirectByteBuffer.<init>(DirectByteBuffer.java:118)
    at java.base/java.nio.ByteBuffer.allocateDirect(ByteBuffer.java:317)
    at org.jboss.xnio@3.7.3.Final//org.xnio.BufferAllocator$2.allocate(BufferAllocator.java:57)
    at org.jboss.xnio@3.7.3.Final//org.xnio.BufferAllocator$2.allocate(BufferAllocator.java:55)
    at org.jboss.xnio@3.7.3.Final//org.xnio.ByteBufferSlicePool.allocateSlices(ByteBufferSlicePool.java:162)
    at org.jboss.xnio@3.7.3.Final//org.xnio.ByteBufferSlicePool.allocate(ByteBufferSlicePool.java:149)
    at io.undertow.core@2.0.27.Final//io.undertow.server.XnioByteBufferPool.allocate(XnioByteBufferPool.java:53)
    at io.undertow.core@2.0.27.Final//io.undertow.server.protocol.framed.AbstractFramedChannel.allocateReferenceCountedBuffer(AbstractFramedChannel.java:549)
    at io.undertow.core@2.0.27.Final//io.undertow.server.protocol.framed.AbstractFramedChannel.receive(AbstractFramedChannel.java:370)
    at io.undertow.core@2.0.27.Final//io.undertow.websockets.core.AbstractReceiveListener.handleEvent(AbstractReceiveListener.java:38)
    at io.undertow.core@2.0.27.Final//io.undertow.websockets.core.AbstractReceiveListener.handleEvent(AbstractReceiveListener.java:33)
    at org.jboss.xnio@3.7.3.Final//org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92)
    at io.undertow.core@2.0.27.Final//io.undertow.server.protocol.framed.AbstractFramedChannel$FrameReadListener.handleEvent(AbstractFramedChannel.java:950)
    at io.undertow.core@2.0.27.Final//io.undertow.server.protocol.framed.AbstractFramedChannel$FrameReadListener.handleEvent(AbstractFramedChannel.java:931)
    at org.jboss.xnio@3.7.3.Final//org.xnio.ChannelListeners.invokeChannelListener(ChannelListeners.java:92)
    at org.jboss.xnio@3.7.3.Final//org.xnio.conduits.ReadReadyHandler$ChannelListenerHandler.readReady(ReadReadyHandler.java:66)
    at org.jboss.xnio.nio@3.7.3.Final//org.xnio.nio.NioSocketConduit.handleReady(NioSocketConduit.java:89)
    at org.jboss.xnio.nio@3.7.3.Final//org.xnio.nio.WorkerThread.run(WorkerThread.java:591)
我们通过 jxray 检查了 JVM 转储,看来 websockets 是罪魁祸首:
jxray report showing a lot of DirectByteBuffer used by websockets
事实上,我们的 websocket 有点简单:
@ApplicationScoped
@ServerEndpoint(value = "/ws/messenger/{accountId}")
public class MessengerSocket implements Serializable
{
    private static final long serialVersionUID = -3173234888004281582L;

    @Inject
    private Logger log;
    @Inject
    private MessengerHandler handler;

    @OnOpen
    public void onOpen(@PathParam("accountId") String accountId, Session session, EndpointConfig config)
    {
        log.debug("Opening for {}", accountId);
        handler.subscribeSocket(session, UUID.fromString(accountId));
    }

    @OnClose
    public void onClose(@PathParam("accountId") String accountId, Session session, CloseReason closeReason)
    {
        log.debug("Closing {}", accountId);
        handler.unsubscribeSocket(session, UUID.fromString(accountId));
    }
}
它与一个简单的处理程序相结合,管理用户 session 的映射:
@ApplicationScoped
public class MessengerHandler
{
    @Inject
    private Logger log;

    // key: Account id
    private Map<UUID, AccountMessengerSessions> sessions;

    public void init()
    {
        sessions = new ConcurrentHashMap<>();
    }

    public void subscribeSocket(Session session, UUID accountId)
    {
        // build and store the account messenger session if new
        AccountMessengerSessions messenger = sessions.getOrDefault(accountId, new AccountMessengerSessions(accountId));
        messenger.getWsSessions().add(session);
        sessions.putIfAbsent(accountId, messenger);
        log.debug("{} has {} messenger socket session(s) (one added)", messenger.getAccountId(), messenger.getWsSessions().size());
    }

    /**
     * Unsubscribes the provided WebSocket from the Messenger.
     */
    public void unsubscribeSocket(Session session, UUID accountId)
    {
        if (!sessions.containsKey(accountId))
        {
            log.warn("Ignore unsubscription from {} socket, as {} is unknwon from messenger", session.getId(), accountId);
            return;
        }
        AccountMessengerSessions messenger = sessions.get(accountId);
        messenger.getWsSessions().remove(session);
        log.debug("{} has {} messenger socket session(s) (one removed)", messenger.getAccountId(), messenger.getWsSessions().size());
        if (!messenger.getWsSessions().isEmpty())
        {
            return;
        }
        // no more socket sessions, fully remove
        sessions.remove(messenger.getAccountId());
    }
}
客户端,我们在页面加载时调用了一些 javascript,同样,没什么特别的:
var accountId = // some string found in DOM
var websocketUrl = "wss://" + window.location.host + "/ws/messenger/" + accountId;
var websocket = new WebSocket(websocketUrl);
websocket.onmessage = function (event) {
  var data = JSON.parse(event.data);
  // nothing fancy here...
};
我们的用户并不经常使用 websocket(即时通讯工具)提供的功能,因此生产中真正发生的事情基本上是 websocket 在每个页面打开和关闭,发送的消息很少。
我们在哪里会出错并造成这种缓冲区泄漏?我们是否忘记了一些重要的事情?

最佳答案

看着这个 post如果您有大量 CPU,则可能会发生这种情况。这是通过减少 IO 工作线程的数量来解决的。不确定这对您的情况是否有帮助。

关于java - OutOfMemoryError : Direct buffer memory when using websockets in WildFly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63519501/

相关文章:

java - 米娜和 Websocket

c# - StringBuilder OutOfMemoryException 异常

cassandra - 如何防止 Cassandra 2.0.11 在启动时内存不足?

c++ - 在内存充足但碎片化的环境下,new/malloc 是否会造成内存洗牌?

java - 如何启动一个新窗口(自身的副本)然后独立关闭每个窗口?在java中

java - 显示 fragment 对话框时如何使应用程序全屏显示

c - 使用 C 语言套接字编程的简单 HTTP 服务器在 Chrome 上失败

angular - 不兼容的 SockJS!主站使用: "1.1.4", iframe : "1.0.0". 怎么处理?

java - ImageView 漂浮在 CardView 的中心

java - Anylogic microsoft sql错误索引1超出范围