Author: davsclaus
Date: Mon Feb 27 07:10:22 2012
New Revision: 1294045
URL: http://svn.apache.org/viewvc?rev=1294045&view=rev
Log:
CAMEL-5045: Fixed memory leak when removing routes.
Modified:
camel/branches/camel-2.8.x/ (props changed)
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Feb 27 07:10:22 2012
@@ -1,2 +1,2 @@
-/camel/branches/camel-2.9.x:1227549,1228229,1229567,1234054,1236672,1238942,1240157,1241006,1241489,1243052,1243058,1244875,1244877,1291871,1292116,1292389,1292726,1292769,1293082,1293935
-/camel/trunk:1226860,1227540,1228223,1229565,1234043,1236667,1238937,1240025,1240950,1240967,1241482,1243046,1243057,1244870,1244872,1291848,1292114,1292384,1292725,1292767,1293079,1293828
+/camel/branches/camel-2.9.x:1227549,1228229,1229567,1234054,1236672,1238942,1240157,1241006,1241489,1243052,1243058,1244875,1244877,1291871,1292116,1292389,1292726,1292769,1293082,1293935,1294044
+/camel/trunk:1226860,1227540,1228223,1229565,1234043,1236667,1238937,1240025,1240950,1240967,1241482,1243046,1243057,1244870,1244872,1291848,1292114,1292384,1292725,1292767,1293079,1293828,1293855
Propchange: camel/branches/camel-2.8.x/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.
Modified:
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
URL:
http://svn.apache.org/viewvc/camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java?rev=1294045&r1=1294044&r2=1294045&view=diff
==============================================================================
---
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
(original)
+++
camel/branches/camel-2.8.x/camel-core/src/main/java/org/apache/camel/management/DefaultManagementLifecycleStrategy.java
Mon Feb 27 07:10:22 2012
@@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -61,6 +62,7 @@ import org.apache.camel.model.OnCompleti
import org.apache.camel.model.OnExceptionDefinition;
import org.apache.camel.model.PolicyDefinition;
import org.apache.camel.model.ProcessorDefinition;
+import org.apache.camel.model.ProcessorDefinitionHelper;
import org.apache.camel.model.RouteDefinition;
import org.apache.camel.processor.interceptor.Tracer;
import org.apache.camel.spi.CamelContextNameStrategy;
@@ -88,6 +90,8 @@ import org.slf4j.LoggerFactory;
public class DefaultManagementLifecycleStrategy implements LifecycleStrategy,
Service, CamelContextAware {
private static final Logger LOG =
LoggerFactory.getLogger(DefaultManagementLifecycleStrategy.class);
+ // the wrapped processors is for performance counters, which are in use
for the created routes
+ // when a route is removed, we should remove the associated processors
from this map
private final Map<Processor, KeyValueHolder<ProcessorDefinition,
InstrumentationProcessor>> wrappedProcessors =
new HashMap<Processor, KeyValueHolder<ProcessorDefinition,
InstrumentationProcessor>>();
private final List<PreRegisterService> preServices = new
ArrayList<PreRegisterService>();
@@ -516,7 +520,14 @@ public class DefaultManagementLifecycleS
} catch (Exception e) {
LOG.warn("Could not unregister Route MBean", e);
}
+
+ // remove from known routes ids, as the route has been removed
+ knowRouteIds.remove(route.getId());
}
+
+ // after the routes has been removed, we should clear the wrapped
processors as we no longer need them
+ // as they were just a provisional map used during creation of routes
+ removeWrappedProcessorsForRoutes(routes);
}
public void onErrorHandlerAdd(RouteContext routeContext, Processor
errorHandler, ErrorHandlerBuilder errorHandlerBuilder) {
@@ -588,7 +599,30 @@ public class DefaultManagementLifecycleS
routeContext.setManagedInterceptStrategy(new
InstrumentationInterceptStrategy(registeredCounters, wrappedProcessors));
}
- @SuppressWarnings("unchecked")
+ /**
+ * Removes the wrapped processors for the given routes, as they are no
longer in use.
+ * <p/>
+ * This is needed to avoid accumulating memory, if a lot of routes is
being added and removed.
+ *
+ * @param routes the routes
+ */
+ private void removeWrappedProcessorsForRoutes(Collection<Route> routes) {
+ // loop the routes, and remove the route associated wrapped
processors, as they are no longer in use
+ for (Route route : routes) {
+ String id = route.getId();
+
+ Iterator<KeyValueHolder<ProcessorDefinition,
InstrumentationProcessor>> it = wrappedProcessors.values().iterator();
+ while (it.hasNext()) {
+ KeyValueHolder<ProcessorDefinition, InstrumentationProcessor>
holder = it.next();
+ RouteDefinition def =
ProcessorDefinitionHelper.getRoute(holder.getKey());
+ if (def != null && id.equals(def.getId())) {
+ it.remove();
+ }
+ }
+ }
+
+ }
+
private void registerPerformanceCounters(RouteContext routeContext,
ProcessorDefinition processor,
Map<ProcessorDefinition,
PerformanceCounter> registeredCounters) {
@@ -669,6 +703,7 @@ public class DefaultManagementLifecycleS
initialized = false;
knowRouteIds.clear();
preServices.clear();
+ wrappedProcessors.clear();
}
/**