Search code examples
scalanettynetty-socketio

Memory raising problem in Netty-Socketio Scala


I use netty-socketio 1.7.18 in my Scala application, but after a while it shutdowns the application with an out of memory error.

I create a simple socketio client with Javascript and I constantly refresh the web page to connect/dissconnect in the browser, the heap size rises very fast(~100mb per reload) and it is never released. It goes up to 130gb.

I tried manual call GC but it doesn't release. When looking from the VisualVM, it seems that the reason are netty buffers.

At first I doubted my own Scala codes. I converted the Java demo for Netty-socketio project to Scala to create a simple server. But I realized that this problem happens when I use the netty-socketio with Scala. Anyone else has this problem? I'm looking for a solution.

package com.wstest

import com.corundumstudio.socketio.{AckRequest, Configuration, SocketIOClient, SocketIOServer}
import com.corundumstudio.socketio.listener.DataListener


object ScalaLauncher {

  @throws[InterruptedException]
  def main(args: Array[String]): Unit = {
    val config = new Configuration
    config.setHostname("localhost")
    config.setPort(9092)
    val server = new SocketIOServer(config)
    server.addEventListener("chatevent", classOf[String], new DataListener[String]() {
      override def onData(client: SocketIOClient, data: String, ackRequest: AckRequest): Unit = { // broadcast messages to all clients
        server.getBroadcastOperations.sendEvent("chatevent", data)
      }
    })
    server.start()
}

Solution

  • I solved with Config.setWorkerThreads(1)
    Each time the page is refreshed, a new nioEventLoopGroup thread was started and the old ones were not closed.