This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-fury.git
The following commit(s) were added to refs/heads/main by this push:
new 18feb0e9 feat(java): revive soft reference (#1585)
18feb0e9 is described below
commit 18feb0e99a1df258adbe3018fbdf4314cf5233c1
Author: Shawn Yang <[email protected]>
AuthorDate: Sat Apr 27 13:28:17 2024 +0800
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.
-->
---
.../src/main/java/org/apache/fury/util/DelayedRef.java | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/java/fury-core/src/main/java/org/apache/fury/util/DelayedRef.java
b/java/fury-core/src/main/java/org/apache/fury/util/DelayedRef.java
index c2254ace..ecb4585c 100644
--- a/java/fury-core/src/main/java/org/apache/fury/util/DelayedRef.java
+++ b/java/fury-core/src/main/java/org/apache/fury/util/DelayedRef.java
@@ -34,7 +34,7 @@ public class DelayedRef<T> {
// If other components doesn't strong hold the referent, this is used
// to cache and get the referent. But the reference will be set to null
// when there is a memory pressure.
- private final SoftReference<T> softRef;
+ private SoftReference<T> softRef;
public DelayedRef(T o) {
weakRef = new WeakReference<>(o);
@@ -43,6 +43,13 @@ public class DelayedRef<T> {
public T get() {
T t = weakRef.get();
- return t != null ? t : softRef.get();
+ if (t != null) {
+ if (softRef.get() == null) {
+ // may be null when a full gc happened.
+ softRef = new SoftReference<>(t);
+ }
+ return t;
+ }
+ return softRef.get();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]