Search code examples
memory-leakspush-notificationpushnettynio

Netty Server Push Usage and Memory Leak


I'm now using Android as my Netty client. And Windows as my Netty Server. Recently, I find a strange behavior on Netty. When I open the server-side app, the memory is only 30MB. But after several hours, it rises up to 300M. Its 10x compared to the original memory usage. The longer I open the server, the more memory it will increase.

I don't know why this is happening. Is it normal?

By the way, since Netty doesn't support built-in server push feature. So I use a static method to store all of the Channel in the map:

public static final Map<Integer, Channel> mapConcurrentIdChannel = new ConcurrentHashMap<Integer, Channel>();

I map the channel ID to Channel. For example: Whenever client A wants to push message to client B, the server will find the channel id and thus get the Channel instance, then use Channel.write(object) method. Is this a correct method to implement Push Message feature in Netty? (If not, could you please suggest a good method to implement Push feature? Since there's no official docs mention that) Also, I'm afraid this implementation causes "Memory Leak Problem" which I explain earlier.

About using ChannelGroup:
My scenario is if there are 5 people, A, B, C, D, E. Sometimes, A wants to send message to C, and sometimes B wants to send message to E. I can't predict when somebody will send message to somebody and who they will send to. So I can't add all the 5 people(connection) to the ChannelGroup, which writing to that group would broadcast the message to everyone.

I've searched on Google for a long time and nothing help on my problem I'm now facing. Wish to hear some recommendations from Netty experienced developers, you!!

Thanks!!


Solution

  • I think you want to use ChannelGroup[1] for this which is basically also just use a ConcurrentMap put ensure the Channel is removed when it is closed etc.

    [1] http://netty.io/3.6/api/org/jboss/netty/channel/group/DefaultChannelGroup.html