klcodanr closed pull request #6: SLING-7586 - using weak references as cache
values to avoid memory leak
URL: https://github.com/apache/sling-org-apache-sling-models-impl/pull/6
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
index 3f50ad8..8d475eb 100644
--- a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
+++ b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
@@ -18,6 +18,7 @@
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
+import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -235,7 +236,7 @@ private void clearDisposalCallbackRegistryQueue() {
// Use threadlocal to count recursive invocations and break recursing if a
max. limit is reached (to avoid cyclic dependencies)
private ThreadLocal<ThreadInvocationCounter> invocationCountThreadLocal;
- private Map<Object, Map<Class, Object>> adapterCache;
+ private Map<Object, Map<Class, WeakReference<Object>>> adapterCache;
private SlingModelsScriptEngineFactory scriptEngineFactory;
@@ -348,11 +349,14 @@ public boolean isModelClass(@Nonnull Class<?> type) {
Model modelAnnotation = modelClass.getModelAnnotation();
if (modelAnnotation.cache()) {
- Map<Class, Object> adaptableCache =
adapterCache.get(adaptable);
+ Map<Class, WeakReference<Object>> adaptableCache =
adapterCache.get(adaptable);
if (adaptableCache != null) {
- ModelType cachedObject = (ModelType)
adaptableCache.get(requestedType);
- if (cachedObject != null) {
- return new Result<>(cachedObject);
+ WeakReference<Object> cachedReference =
adaptableCache.get(requestedType);
+ if (cachedReference != null) {
+ ModelType cachedObject = (ModelType)
cachedReference.get();
+ if (cachedObject != null) {
+ return new Result<>(cachedObject);
+ }
}
}
}
@@ -377,12 +381,12 @@ public boolean isModelClass(@Nonnull Class<?> type) {
ModelType model = (ModelType)
Proxy.newProxyInstance(modelClass.getType().getClassLoader(), new Class<?>[] {
modelClass.getType() }, handlerResult.getValue());
if (modelAnnotation.cache()) {
- Map<Class, Object> adaptableCache =
adapterCache.get(adaptable);
+ Map<Class, WeakReference<Object>> adaptableCache =
adapterCache.get(adaptable);
if (adaptableCache == null) {
adaptableCache = new
ConcurrentHashMap<>(INNER_CACHE_INITIAL_CAPACITY);
adapterCache.put(adaptable, adaptableCache);
}
- adaptableCache.put(requestedType, model);
+ adaptableCache.put(requestedType, new
WeakReference(model));
}
result = new Result<>(model);
@@ -394,12 +398,12 @@ public boolean isModelClass(@Nonnull Class<?> type) {
result = createObject(adaptable, modelClass);
if (result.wasSuccessful() && modelAnnotation.cache())
{
- Map<Class, Object> adaptableCache =
adapterCache.get(adaptable);
+ Map<Class, WeakReference<Object>> adaptableCache =
adapterCache.get(adaptable);
if (adaptableCache == null) {
adaptableCache = new
ConcurrentHashMap<>(INNER_CACHE_INITIAL_CAPACITY);
adapterCache.put(adaptable, adaptableCache);
}
- adaptableCache.put(requestedType,
result.getValue());
+ adaptableCache.put(requestedType, new
WeakReference(result.getValue()));
}
} catch (Exception e) {
String msg = String.format("Unable to create model
%s", modelClass.getType());
@@ -1065,7 +1069,7 @@ protected ThreadInvocationCounter initialValue() {
}
};
- this.adapterCache = Collections.synchronizedMap(new
WeakHashMap<Object, Map<Class, Object>>());
+ this.adapterCache = Collections.synchronizedMap(new
WeakHashMap<Object, Map<Class, WeakReference<Object>>>());
BundleContext bundleContext = ctx.getBundleContext();
this.queue = new ReferenceQueue<>();
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services