Fix memory leak in BA mount service 03/81903/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 5 May 2019 18:15:19 +0000 (20:15 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 6 May 2019 08:05:29 +0000 (08:05 +0000)
The cache used in BA mount service uses weakKeys() so the keys
(DOM mountpoint) could be GCed when no except cache, referenced
them).

However the values in the cache (BA mountpoint) also kept a reference
to the key, so the key was never weakly reachable and thus never GCed
resulting in a memory leak.

This is especially visible in case of frequent reconnects of netconf
mountpoints where after every reconnect a new DOM mountpoint is
created, requiring a new BA mountpoint creating new entry in the
cache of BA mount service.

This patch updates the entire suite of adapters, as it is shared
across all instances.

Change-Id: I9c109dd6d499d6185842d6a1ad5a59d75565af5c
Signed-off-by: Maros Marsalek <mmarsalek@frinx.io>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 8ae89c54f8019f53327001d1b916a53963097b34)

binding/mdsal-binding-dom-adapter/src/main/java/org/opendaylight/mdsal/binding/dom/adapter/AbstractBindingLoadingAdapter.java

index 97a28524de8e9fa83550bcb5d45c4c071b38eec3..2f960c034dbe30e91bd6e639c5b32ef09649cfa4 100644 (file)
@@ -14,12 +14,13 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
 
 @NonNullByDefault
 abstract class AbstractBindingLoadingAdapter<D, K, V> extends AbstractBindingAdapter<D> {
-    private final LoadingCache<K, V> proxies = CacheBuilder.newBuilder().weakKeys().build(new CacheLoader<K, V>() {
-        @Override
-        public V load(final K key) {
-            return loadAdapter(key);
-        }
-    });
+    private final LoadingCache<K, V> proxies = CacheBuilder.newBuilder().weakKeys().weakValues().build(
+        new CacheLoader<K, V>() {
+            @Override
+            public V load(final K key) {
+                return loadAdapter(key);
+            }
+        });
 
     AbstractBindingLoadingAdapter(final BindingToNormalizedNodeCodec codec, final D delegate) {
         super(codec, delegate);