Repository: telstra/open-kilda
Issue: Server42: stats app leaks memory if no server42 flows were created

After ~1h, if no server42 flows are created, the `server42-stats` container runs out of memory.
Steps:
1. Start kilda and just leave it for hour+. Do not create any flows
2. call `docker logs server42-stats`
[server42_stats_oom.zip](https://github.com/telstra/open-kilda/files/5416162/server42_stats_oom.zip)
**Expected:** stats app works just fine
**Actual:** java.lang.OutOfMemoryError: Java heap space

Changed files:
src-java/server42/server42-control-server-stub/src/main/java/org/openkilda/server42/control/serverstub/StatsServer.java
src-java/server42/server42-stats/src/main/java/org/openkilda/server42/stats/zeromq/StatsCollector.java

diff --git a/src-java/server42/server42-control-server-stub/src/main/java/org/openkilda/server42/control/serverstub/StatsServer.java b/src-java/server42/server42-control-server-stub/src/main/java/org/openkilda/server42/control/serverstub/StatsServer.java
index 7a5200d52..873ee1998 100644
--- a/src-java/server42/server42-control-server-stub/src/main/java/org/openkilda/server42/control/serverstub/StatsServer.java
+++ b/src-java/server42/server42-control-server-stub/src/main/java/org/openkilda/server42/control/serverstub/StatsServer.java
@@ -87,8 +87,10 @@ public class StatsServer extends Thread {
                     }
                 }
 
-                if (!flows.isEmpty()) {
-                    server.send(flowBucketBuilder.build().toByteArray());
+                server.send(flowBucketBuilder.build().toByteArray());
+                if (flows.isEmpty()) {
+                    log.info("send ping");
+                } else {
                     log.info("send stats");
                 }
 
diff --git a/src-java/server42/server42-stats/src/main/java/org/openkilda/server42/stats/zeromq/StatsCollector.java b/src-java/server42/server42-stats/src/main/java/org/openkilda/server42/stats/zeromq/StatsCollector.java
index 133e55d5f..5337f7216 100644
--- a/src-java/server42/server42-stats/src/main/java/org/openkilda/server42/stats/zeromq/StatsCollector.java
+++ b/src-java/server42/server42-stats/src/main/java/org/openkilda/server42/stats/zeromq/StatsCollector.java
@@ -73,7 +73,10 @@ public class StatsCollector extends Thread {
         sessionId = RandomStringUtils.randomAlphanumeric(8);
         log.info("started with session id {}", sessionId);
         while (!isInterrupted()) {
-            try (Socket server = context.createSocket(ZMQ.PULL)) {
+
+            Socket server = null;
+            try {
+                server = context.createSocket(ZMQ.PULL);
                 server.setReceiveTimeOut(1000);
                 server.connect(connectEndpoint);
                 log.info("connect to {}", connectEndpoint);
@@ -90,11 +93,13 @@ public class StatsCollector extends Thread {
                     log.debug("stats received");
                     handleInput(recv);
                 }
-
             } catch (org.zeromq.ZMQException ex) {
                 log.error(ex.toString());
             } finally {
                 log.info("disconnected");
+                if (server != null) {
+                    context.destroySocket(server);
+                }
             }
         }
     }