[#2390] Minimize memory usage of NioDatagramChannel

Motivation:
At the moment we create a HashMap that holds the MembershipKeys for multicast with every NioDatagramChannel even when most people not need it at al

Modifications:
Lazy create the HashMap when needed.

Result:
Less memory usage and less object creation
This commit is contained in:
Norman Maurer 2014-04-15 06:40:15 +02:00
parent 8615f7a69e
commit 238af8cbd1

View File

@ -65,9 +65,8 @@ public final class NioDatagramChannel
private static final SelectorProvider DEFAULT_SELECTOR_PROVIDER = SelectorProvider.provider();
private final DatagramChannelConfig config;
private final Map<InetAddress, List<MembershipKey>> memberships =
new HashMap<InetAddress, List<MembershipKey>>();
private Map<InetAddress, List<MembershipKey>> memberships;
private RecvByteBufAllocator.Handle allocHandle;
private static DatagramChannel newSocket(SelectorProvider provider) {
@ -368,7 +367,12 @@ public final class NioDatagramChannel
}
synchronized (this) {
List<MembershipKey> keys = memberships.get(multicastAddress);
List<MembershipKey> keys = null;
if (memberships == null) {
memberships = new HashMap<InetAddress, List<MembershipKey>>();
} else {
keys = memberships.get(multicastAddress);
}
if (keys == null) {
keys = new ArrayList<MembershipKey>();
memberships.put(multicastAddress, keys);