From f779c199ea272cf61781e120b1ab2efc50de0cbb Mon Sep 17 00:00:00 2001 From: Dridi Boukelmoune Date: Mon, 16 Apr 2012 20:17:39 +0300 Subject: [PATCH] Fix scoped-proxy memory leak w/ @Resource injection Prior to this change, request-scoped components having @Resource-injected dependencies caused a memory leak in DefaultListableBeanFactory#dependenciesForBeanMap. Consider the following example: @Component @Scope(value="request", proxyMode=ScopedProxyMode.TARGET_CLASS) public class MyComponent { @Resource private HttpServletRequest request; // ... } The bean name for "MyComponent" will end up being 'scopedTarget.myComponent', which will become a key in the #dependenciesForBeanMap structure. On the first request, the injected HttpServletRequest bean will be a proxy and will internally have a bean name of the form "$Proxy10@1a3a2a52". This name will be added to the Set value associated with the 'scopedTarget.myComponent' entry in #dependenciesForBeanMap. On the second request, the process will repeat, but the injected HttpServletRequest will be a different proxy instance, thus having a different identity hex string, e.g. "$Proxy10@5eba06ff". This name will also be added to the Set value associated with the 'scopedTarget.myComponent' entry in #dependenciesForBeanMap, and this is the source of the leak: a new entry is added to the set on each request but should be added only once. This commit fixes the leak by introducing caching to CommonAnnotationBeanPostProcessor#ResourceElement similar to that already present in AutowiredAnnotationBeanPostProcessor#AutowiredFieldElement and #AutowiredMethodElement. Essentially, each ResourceElement instance now tracks whether it has been created, caches the ultimate value to be injected and returns it eagerly if necessary. Besides solving the memory leak, this has the side effect of avoiding unnecessary proxy creation. This fix also explains clearly why injection into request-scoped components using @Autowired never suffered this memory leak: because the correct caching was already in place. Because @Resource is considerably less-frequently used than @Autowired, and given that this particular injection arrangement is relatively infrequent, it becomes understandable how this bug has been present without being reported since the introduction of @Resource support in Spring 2.5: developers were unlikely to encounter it in the first place; and if they did, the leak was minor enough (adding strings to a Set), that it could potentially go unnoticed indefinitely depending on request volumes and available memory. Issue: SPR-9176 --- .../CommonAnnotationBeanPostProcessor.java | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java index 8478cf6f2ff..23995bcac9e 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,6 +35,7 @@ import java.util.LinkedList; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; + import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; @@ -509,9 +510,12 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean */ private class ResourceElement extends LookupElement { - @SuppressWarnings("unused") protected boolean shareable = true; + private volatile boolean cached = false; + + private volatile Object cachedFieldValue; + public ResourceElement(Member member, PropertyDescriptor pd) { super(member, pd); } @@ -546,7 +550,20 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean @Override protected Object getResourceToInject(Object target, String requestingBeanName) { - return getResource(this, requestingBeanName); + Object value = null; + if (this.cached && this.shareable) { + value = this.cachedFieldValue; + } + synchronized (this) { + if (!this.cached) { + value = getResource(this, requestingBeanName); + if (value != null && this.shareable) { + this.cachedFieldValue = value; + this.cached = true; + } + } + } + return value; } } @@ -724,4 +741,4 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean } } -} +} \ No newline at end of file