OutOfMemoryError:在WildFly中使用WebSockets时直接缓冲内存 [英] OutOfMemoryError: Direct buffer memory when using websockets in WildFly

查看:68
本文介绍了OutOfMemoryError:在WildFly中使用WebSockets时直接缓冲内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的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是罪魁祸首:

事实是我们的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));
    }
}

它与一个简单的处理程序配合使用,用于管理用户会话映射:

@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(即时通讯程序)提供的功能,所以生产中实际发生的基本上是WebSockets在每个页面打开和关闭,发送的消息很少。

我们会在哪里出错并造成此缓冲区泄漏?我们是不是忘了什么重要的东西?

推荐答案

我在我们的野蝇18上也有类似的问题(野蝇19也有这个问题)。它可能是由WildFly内的faultyxnio lib触发的。更新到WildFly 22(使用最新的xnio库)后,问题消失了。

这篇关于OutOfMemoryError:在WildFly中使用WebSockets时直接缓冲内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆