DataSchemaContextTree contains an internal weak-keyed cache, which is
used to speed up lookups and share DataSchemaContextNodes.
Unfortunately the cache uses strong references for values (e.g.
DataSchemaContextTree) instances, which transitively (via
DataSchemaContextNode) retain the reference to the key. This has the
effect of the key being always reachable, thus causing a memory leak.
Make the cache use weak references for values, which ensures that it
gets scavenged whenever its contents are no longer referenced.
Change-Id: I1684d1bf2c2c5cdc44cda1e8e508149ff3a8fae4
Signed-off-by: Robert Varga <rovarga@cisco.com>
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
-import java.util.Iterator;
import javax.annotation.Nonnull;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import javax.annotation.Nonnull;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
public final class DataSchemaContextTree {
private static final LoadingCache<SchemaContext, DataSchemaContextTree> TREES = CacheBuilder.newBuilder()
public final class DataSchemaContextTree {
private static final LoadingCache<SchemaContext, DataSchemaContextTree> TREES = CacheBuilder.newBuilder()
- .weakKeys()
- .build(new CacheLoader<SchemaContext, DataSchemaContextTree>() {
-
+ .weakKeys().weakValues().build(new CacheLoader<SchemaContext, DataSchemaContextTree>() {
@Override
public DataSchemaContextTree load(final SchemaContext key) throws Exception {
return new DataSchemaContextTree(key);
}
@Override
public DataSchemaContextTree load(final SchemaContext key) throws Exception {
return new DataSchemaContextTree(key);
}
});
private final DataSchemaContextNode<?> root;
});
private final DataSchemaContextNode<?> root;
public DataSchemaContextNode<?> getChild(final YangInstanceIdentifier path) {
DataSchemaContextNode<?> currentOp = root;
public DataSchemaContextNode<?> getChild(final YangInstanceIdentifier path) {
DataSchemaContextNode<?> currentOp = root;
- Iterator<PathArgument> arguments = path.getPathArguments().iterator();
- while (arguments.hasNext()) {
- currentOp = currentOp.getChild(arguments.next());
+ for (PathArgument arg : path.getPathArguments()) {
+ currentOp = currentOp.getChild(arg);