Saturday, July 4, 2015

Reading Message Broker property value has cause memory leak

This is my first time encountering memory leak happened in Java code. I’m feeling so ridiculous about this because I always believe that Java is a language that I should not worry about memory leak. There is one web service I was working on have spotted an abnormal behaviour usage and eventually will force the Execution Group stop functioning. This abnormal behaviour is hard to spot on my own until I run a load test on it (the load test was execute with SOAP UI).

But later have found out the root cause was happened when reading the properties values from configurable service defined in Message Broker. As according to the documentation, whenever reading the properties values with BrokerProxy as shown in the following code, it is advisable to invoke brokerProxy.disconnect() to free up resources, otherwise the memory will keep piling up causing memory leaks.
    public class JavaNode extends MbJavaComputeNode {
    
     @Override
     public void evaluate(MbMessageAssembly inAssembly) throws MbException
     {
      BrokerProxy brokerProxy;
      try {
       brokerProxy = BrokerProxy.getLocalInstance();
       brokerProxy.getConfigurableService("USERDEFINED", "key");
      } catch (ConfigManagerProxyLoggedException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
      } catch (ConfigManagerProxyPropertyNotInitializedException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
    
      ...
     }
    }

Thus what I did is to invoke the disconnect() in finally clause. But have to be aware that after the resources had been freed, brokerProxy will no longer usable and have to be renew. Below are some useful resources I have read in solving the issue:
  1. Important facts about garbage collection in WebSphere Message Broker
  2. Class BrokerProxy
  3. Connecting to a broker from a CMP application

No comments: