I was profiling my OSGi App with YourKit, using a heap dump I've made of my App that is running for 44 hours.
I suddenly realize that the class EventDispatcher is huge in terms of occupied memory... What can I do to solve this? This makes the entire App to take up more than 1GB of RAM.
I don't even remember directly using that OSGi class. Need help.
EDIT: I eventually ended up looking for that variable m_svcListeners in Felix's source code, which you can get here, just import as an existing Maven project into Eclipse.
The class of interest is org.apache.felix.framework.util.EventDispatcher.
Well I've searched for the methods which can be responsible for making the variable m_svcListeners (which is a Map) to grow. So this being a map, I looked for methods which manipulate this variable and use its .put() method. These are updateListener() and addListener().
They make a new copy of m_svcListeners each time they modify it so the trick is not to look for m_svcListeners.put but for m_svcListeners = ...
Now I will ask Eclipse to show me the calls to these two methods, this is what I get:
Calls for addListener()
Calls for updateListener()
With this, I compiled my own version of Felix using mvn install (however he didn't create a main class not even a main entry in the manifest file, maybe mvn install isn't the correct way to build felix), and added the main. I inserted a stack trace each time the addListener() method is invoked and I've found that every time we invoke a setContext(BundleContext b) he tries to add a listener:
at org.apache.felix.framework.util.EventDispatcher.addListener(EventDispatcher.java:159) at org.apache.felix.framework.Felix.addServiceListener(Felix.java:3328) at org.apache.felix.framework.BundleContextImpl.addServiceListener(BundleContextImpl.java:279) at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:294) at org.osgi.util.tracker.ServiceTracker.open(ServiceTracker.java:261) at shared.osgi.services.internal.OSGiServiceRegistryImpl.setBundleContext(OSGiServiceRegistryImpl.java:213) (...)
I also made a thread print the size of the map every 2 seconds and so far it reached 15 in size. I will wait 24h to see what changes.