Uploaded image for project: 'Camel'
  1. Camel
  2. CAMEL-12969

camel-core-osgi: Slow Memory Leak in OsgiServiceRegistry

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.18.0, 2.19.0, 2.20.0, 2.21.0, 2.22.0, 2.23.0
    • 2.21.4, 2.22.3, 2.23.1, 2.24.0
    • camel-osgi
    • None
    • Java 10

      Karaf 4.2.1

      Camel 2.22.0

    • Patch Available
    • Unknown
    • Patch

    Description

      The OsgiServiceRegistry has a slow memory leak in the serviceReferenceQueue.  Currently every time a service is looked up by any method an item is added to the serviceReferenceQueue.  This is required because of OSGi ServiceReference counting.  However left unchecked the system just continues to add ConcurrentLinkedQueue$Node objects until memory is exhausted.

       

      There is also a second problem with how the registry is being managed within the OsgiDefaultCamelContext.  OsgiServiceRegistry is currently extends LifecycleStrategySupport which is suppose to unload the serviceReferenceQueue onContextStop.  However the registry is never getting added to the CamelContext to manage the Lifecycle because the overridden createRegistry method in OsgiDefaultCamelContext is not being called.  This is because the registry is being set in the constructor of OsgiDefaultCamelContext with

      super(registry);

      this calls the DefaultCamelContext implementation of createRegistry which does not add the registry to lifecyclemanagement since

      OsgiCamelContextHelper.wrapRegistry(this, registry, bundleContext);

      is never called. 

      See serviceReferenceQueue  pre context stop

       

      See serviceReferenceQueue   post context stop (still contain objects)

       

      Both issues would have existed for some time but may have gone unnoticed because the leak was so slow (ConcurrentLinkedQueue$Node takes up very little memory).  It appears the removal of the cache in https://issues.apache.org/jira/browse/CAMEL-9631 makes the leak occur more quickly. 

       

      I have a patch that involves reintroducing the cache but with an invalidation strategy using the OSGi ServiceListener that leverages a single clean up thread to remain non-blocking.  I'm working on an upstream adaptation and will post a PR for community review.

       

       

      Attachments

        1. ServiceReferenceQueuePostContextStop.PNG
          50 kB
          Bob Paulin
        2. karafCamelContextStop.PNG
          5 kB
          Bob Paulin
        3. ServiceReferenceQueuePreContextStop.PNG
          50 kB
          Bob Paulin
        4. ServiceReferenceQueueLeak.PNG
          13 kB
          Bob Paulin

        Activity

          People

            davsclaus Claus Ibsen
            bobpaulin Bob Paulin
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 20m
                20m