Skip to content

Commit dcafabb

Browse files
committed
[memory] SoftReference for ResourceCompilationUnit.contents #1743
Ability to reduce memory during searches that find many files #1743
1 parent a9f1280 commit dcafabb

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

org.eclipse.jdt.core/model/org/eclipse/jdt/internal/core/util/ResourceCompilationUnit.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.internal.core.util;
1515

16+
import java.lang.ref.SoftReference;
17+
1618
import org.eclipse.core.resources.IFile;
1719
import org.eclipse.core.runtime.CoreException;
1820
import org.eclipse.jdt.core.compiler.CharOperation;
@@ -24,7 +26,7 @@
2426
public class ResourceCompilationUnit implements ICompilationUnit {
2527

2628
private final IFile file;
27-
private char[] contents;
29+
private volatile SoftReference<char[]> contentRef;
2830
private final char[] fileName;
2931
private final char[] mainTypeName;
3032
private final char[] module;
@@ -46,15 +48,22 @@ public ResourceCompilationUnit(IFile file, char[] mod) {
4648

4749
@Override
4850
public char[] getContents() {
49-
if (this.contents != null)
50-
return this.contents; // answer the cached source
51-
52-
// otherwise retrieve it
51+
SoftReference<char[]> cr = this.contentRef;
52+
if (cr != null) {
53+
char[] cachedContents = cr.get();
54+
if (cachedContents != null) {
55+
return cachedContents;
56+
}
57+
}
58+
char[] contents;
5359
try {
54-
return (this.contents = Util.getResourceContentsAsCharArray(this.file));
60+
contents = Util.getResourceContentsAsCharArray(this.file);
5561
} catch (CoreException e) {
56-
return CharOperation.NO_CHAR;
62+
contents = CharOperation.NO_CHAR;
5763
}
64+
// softly cache the result:
65+
this.contentRef = new SoftReference<>(contents);
66+
return contents;
5867
}
5968

6069
@Override

0 commit comments

Comments
 (0)