Skip to content

Commit 18feb0e

Browse files
authored
feat(java): revive soft reference (#1585)
## What does this PR do? This PR revives soft reference to try to make it has a longer lifetime ## Related issues <!-- Is there any related issue? Please attach here. - #xxxx0 - #xxxx1 - #xxxx2 --> ## Does this PR introduce any user-facing change? <!-- If any user-facing interface changes, please [open an issue](https://github.com/apache/incubator-fury/issues/new/choose) describing the need to do so and update the document if necessary. --> - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark <!-- When the PR has an impact on performance (if you don't know whether the PR will have an impact on performance, you can submit the PR first, and if it will have impact on performance, the code reviewer will explain it), be sure to attach a benchmark data here. -->
1 parent 007e8df commit 18feb0e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

java/fury-core/src/main/java/org/apache/fury/util/DelayedRef.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class DelayedRef<T> {
3434
// If other components doesn't strong hold the referent, this is used
3535
// to cache and get the referent. But the reference will be set to null
3636
// when there is a memory pressure.
37-
private final SoftReference<T> softRef;
37+
private SoftReference<T> softRef;
3838

3939
public DelayedRef(T o) {
4040
weakRef = new WeakReference<>(o);
@@ -43,6 +43,13 @@ public DelayedRef(T o) {
4343

4444
public T get() {
4545
T t = weakRef.get();
46-
return t != null ? t : softRef.get();
46+
if (t != null) {
47+
if (softRef.get() == null) {
48+
// may be null when a full gc happened.
49+
softRef = new SoftReference<>(t);
50+
}
51+
return t;
52+
}
53+
return softRef.get();
4754
}
4855
}

0 commit comments

Comments
 (0)