While doing some memory profiling on a custom application using DOSGi, I noticed after a while there were many existing ZooKeeper instances, with their references being held by multiple registered PublishingEndpointListenerFactory services and some LocalDiscovery references too. Further investigation found several issues:
1. PublishingEndpointListenerFactory was registered as a service when started, but did not unregister the service when stopped (the bundle itself does not get deactivated, so there's no automatic unregistering).
2. ZookeeperDiscovery was re-creating a new instance of PublishingEndpointListenerFactory (and the rest of its implementation classes, registrations, etc.) whenever it received a ZooKeeper SyncConnected event, however these can be received more than once in a row (e.g. with a unhandled Disconnected event in between), thus leaking copies of everything it initializes.
3. LocalDiscovery was missing synchronization code, and also had a little memory leak (filter strings of removed endpoint listeners were never removed from its internal data structures).
All issues are now fixed, and these memory/service leaks no longer occur.