diff --git a/platform/core-impl/src/com/intellij/util/SofterReference.java b/platform/core-impl/src/com/intellij/util/SofterReference.java new file mode 100644 index 000000000000..e15ef2ccabff --- /dev/null +++ b/platform/core-impl/src/com/intellij/util/SofterReference.java @@ -0,0 +1,69 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util; + +import com.intellij.openapi.util.LowMemoryWatcher; +import com.intellij.util.containers.WeakList; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.ref.Reference; +import java.lang.ref.SoftReference; +import java.lang.ref.WeakReference; + +/** + * A reference whose referent may be garbage-collected when there's low free memory, not only when there's none. + * Use for objects which retain lots of memory and whose loss is not very expensive. + * + * @author peter + */ +public class SofterReference { + private volatile Reference myRef; + private static final WeakList ourRegistry = new WeakList(); + + @SuppressWarnings("UnusedDeclaration") private static LowMemoryWatcher ourWatcher = LowMemoryWatcher.register(new Runnable() { + @Override + public void run() { + System.out.println("SofterReference.run: " + ourRegistry.size()); + for (SofterReference reference : ourRegistry.copyAndClear()) { + reference.weaken(); + } + } + }); + + public SofterReference(@NotNull T referent) { + ourRegistry.add(this); + myRef = new SoftReference(referent); + } + + private void weaken() { + T o = myRef.get(); + if (o != null) { + myRef = new WeakReference(o); + } + } + + @Nullable + public T get() { + Reference ref = myRef; + T referent = ref.get(); + if (referent != null && ref instanceof WeakReference) { + ourRegistry.add(this); + myRef = new SoftReference(referent); + } + return referent; + } +} diff --git a/platform/util/src/com/intellij/util/containers/WeakList.java b/platform/util/src/com/intellij/util/containers/WeakList.java index 7fb01f556302..22890840ba33 100644 --- a/platform/util/src/com/intellij/util/containers/WeakList.java +++ b/platform/util/src/com/intellij/util/containers/WeakList.java @@ -102,6 +102,15 @@ public class WeakList extends UnsafeWeakList { } } + public List copyAndClear() { + synchronized (myArray) { + List result = new ArrayList(myArray.size()); + myArray.toStrongCollection(result); + clear(); + return result; + } + } + private class MySyncIterator extends MyIterator { protected void findNext() { synchronized (myArray) {