Author: uschindler
Date: Thu Oct 4 21:59:38 2012
New Revision: 1394293
URL: http://svn.apache.org/viewvc?rev=1394293&view=rev
Log:
Merged revision(s) 1394291 from lucene/dev/trunk:
LUCENE-4459: Improve WeakIdentityMap.keyIterator() to remove GCed keys from backing map early
instead of waiting for reap(). This makes test failures in TestWeakIdentityMap disappear,
too.
Modified:
lucene/dev/branches/branch_4x/ (props changed)
lucene/dev/branches/branch_4x/lucene/ (props changed)
lucene/dev/branches/branch_4x/lucene/CHANGES.txt (contents, props changed)
lucene/dev/branches/branch_4x/lucene/core/ (props changed)
lucene/dev/branches/branch_4x/lucene/core/src/java/org/apache/lucene/util/WeakIdentityMap.java
lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/TestWeakIdentityMap.java
Modified: lucene/dev/branches/branch_4x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/CHANGES.txt?rev=1394293&r1=1394292&r2=1394293&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_4x/lucene/CHANGES.txt Thu Oct 4 21:59:38 2012
@@ -33,6 +33,11 @@ Optimizations
into the skipdata. You need to reindex any indexes created with
this experimental codec. (Robert Muir)
+* LUCENE-4459: Improve WeakIdentityMap.keyIterator() to remove GCed keys
+ from backing map early instead of waiting for reap(). This makes test
+ failures in TestWeakIdentityMap disappear, too.
+ (Uwe Schindler, Mike McCandless, Robert Muir)
+
Build
* LUCENE-4451: Memory leak per unique thread caused by
Modified: lucene/dev/branches/branch_4x/lucene/core/src/java/org/apache/lucene/util/WeakIdentityMap.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/core/src/java/org/apache/lucene/util/WeakIdentityMap.java?rev=1394293&r1=1394292&r2=1394293&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/core/src/java/org/apache/lucene/util/WeakIdentityMap.java
(original)
+++ lucene/dev/branches/branch_4x/lucene/core/src/java/org/apache/lucene/util/WeakIdentityMap.java
Thu Oct 4 21:59:38 2012
@@ -133,22 +133,22 @@ public final class WeakIdentityMap<K,V>
@Override
public boolean hasNext() {
- return nextIsSet ? true : setNext();
+ return nextIsSet || setNext();
}
@Override @SuppressWarnings("unchecked")
public K next() {
- if (nextIsSet || setNext()) {
- try {
- assert nextIsSet;
- return (K) next;
- } finally {
- // release strong reference and invalidate current value:
- nextIsSet = false;
- next = null;
- }
+ if (!hasNext()) {
+ throw new NoSuchElementException();
+ }
+ assert nextIsSet;
+ try {
+ return (K) next;
+ } finally {
+ // release strong reference and invalidate current value:
+ nextIsSet = false;
+ next = null;
}
- throw new NoSuchElementException();
}
@Override
@@ -161,14 +161,15 @@ public final class WeakIdentityMap<K,V>
while (iterator.hasNext()) {
next = iterator.next().get();
if (next == null) {
- // already garbage collected!
- continue;
- }
- // unfold "null" special value
- if (next == NULL) {
- next = null;
+ // the key was already GCed, we can remove it from backing map:
+ iterator.remove();
+ } else {
+ // unfold "null" special value:
+ if (next == NULL) {
+ next = null;
+ }
+ return nextIsSet = true;
}
- return nextIsSet = true;
}
return false;
}
Modified: lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/TestWeakIdentityMap.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/TestWeakIdentityMap.java?rev=1394293&r1=1394292&r2=1394293&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/TestWeakIdentityMap.java
(original)
+++ lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/TestWeakIdentityMap.java
Thu Oct 4 21:59:38 2012
@@ -122,13 +122,16 @@ public class TestWeakIdentityMap extends
for (int i = 0; size > 0 && i < 10; i++) try {
System.runFinalization();
System.gc();
+ int newSize = map.size();
+ assertTrue("previousSize("+size+")>=newSize("+newSize+")", size >= newSize);
+ size = newSize;
Thread.sleep(100L);
c = 0;
for (Iterator<String> it = map.keyIterator(); it.hasNext();) {
assertNotNull(it.next());
c++;
}
- final int newSize = map.size();
+ newSize = map.size();
assertTrue("previousSize("+size+")>=iteratorSize("+c+")", size >= c);
assertTrue("iteratorSize("+c+")>=newSize("+newSize+")", c >= newSize);
size = newSize;
@@ -223,13 +226,16 @@ public class TestWeakIdentityMap extends
for (int i = 0; size > 0 && i < 10; i++) try {
System.runFinalization();
System.gc();
+ int newSize = map.size();
+ assertTrue("previousSize("+size+")>=newSize("+newSize+")", size >= newSize);
+ size = newSize;
Thread.sleep(100L);
int c = 0;
for (Iterator<Object> it = map.keyIterator(); it.hasNext();) {
assertNotNull(it.next());
c++;
}
- final int newSize = map.size();
+ newSize = map.size();
assertTrue("previousSize("+size+")>=iteratorSize("+c+")", size >= c);
assertTrue("iteratorSize("+c+")>=newSize("+newSize+")", c >= newSize);
size = newSize;
|