Java:寻找可变/可重引用的弱引用实现

发布于 2024-10-17 00:42:14 字数 446 浏览 4 评论 0 原文

我正在寻找类似于 java.lang.ref.WeakReference 的弱引用实现,但它提供了 set() 方法或其他重新引用创建的方法弱引用对象。这是一个例子:

MutableWeakReference ref = new MutableWeakReference(someObject);
ref.set(anotherObject);

我需要这个来避免对象创建,在我的例子中,这会使执行时间减慢一个数量级,因为我不断地更改弱引用引用的对象。

我尝试从 JDK 复制代码,但这似乎不可能,因为 java.lang.ref.Reference 使用内部的 sun.misc.Cleaner 类。我还查看了 Android 实现,但似乎它依赖于 Dalvik VM 进行垃圾收集。我想知道这是否真的可以在不改变 JVM/环境的情况下实现。

I am looking for a weak reference implementation similar to java.lang.ref.WeakReference, but which offers a set() method or some other way of re-reference the created weak reference object. Here is the example:

MutableWeakReference ref = new MutableWeakReference(someObject);
ref.set(anotherObject);

I need this to avoid object creation which, in my case slows down the execution time by an order of magnitude, because I am constantly changing the object to which my weak reference refers.

I tried to copy the code from JDK, but it seems impossible since java.lang.ref.Reference uses the sun.misc.Cleaner class which is internal. I also looked on Android implementation but it seems it depends on Dalvik VM for Garbage collection. I wonder if this is actually possible to implement without changing the JVM / environment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没有 2024-10-24 00:42:14

我实际上正在实现某种迭代器。每当我前进到下一个条目时,我都需要创建一个新的 WeakReference

我很困惑为什么您需要在迭代器中使用 WeakReferenceWeakReference 的正常用例是对对象的长期引用。但迭代器通常是短期对象,迭代通常是短期过程。事实上,它/它们在短期内使用对目标对象的普通(强)引用不应该是一个问题。

我已经运行了一些测试,看起来速度慢了 8-10 倍。

同样,这表明您根本不应该使用 WeakReference

是否有特殊原因导致常规参考资料对您不起作用?

I am actually implementing an iterator of some kind. Whenever I advance to the next entry, I need to create a new WeakReference.

I'm puzzled why you would need to use a WeakReference at all in an iterator. The normal use-case for WeakReference is for long term references to objects. But an iterator is typically a short term object, and an iteration is typically a short term process. The fact that it / they use an ordinary (strong) reference to the target object in the short term shouldn't be a concern.

I have run some tests and it seems this is like 8-10 times slower.

Again, this suggests that you shouldn't be using a WeakReference at all.

Is there a particular reason why won't a regular reference work for you?

只想待在家 2024-10-24 00:42:14

难道不是可以将您的引用封装在一个简单的文件中

class MyOwnReference<T> {
    public T ref;
    public void set(T o) { ref = o; }
}

并创建 WeakReference> 吗?

我想知道这是否真的可以在不改变 JVM/环境的情况下实现。

不,您可能无法“重新实现”WeakReference。它是 JVM 支持的类。

您确定是 WeakReference 实例的创建导致速度减慢吗?我不认为做

ref = new WeakReference(someObject);

一些事情

ref.set(anotherObject);

会更昂贵。

Wouldn't it just be possible to encapsulate your references in a simple

class MyOwnReference<T> {
    public T ref;
    public void set(T o) { ref = o; }
}

and create WeakReference<MyOwnReference<WhatEver>>?

I wonder if this is actually possible to implement without changing the JVM / environment.

No, you probably can't "reimplement" the WeakReference. It is a JVM-supported class.

Are you sure it is the creation of WeakReference instances that slows it down? I wouldn't think doing

ref = new WeakReference(someObject);

instead of some

ref.set(anotherObject);

would be that much more expensive.

没︽人懂的悲伤 2024-10-24 00:42:14

您无法自己编写弱引用。它是 JVM 专门处理的“特殊”类。

只需使用新的弱引用

class MutableWeakReference<T> 

    WeakReference<T> wr;

    void set(T obj)
        wr = new WeakReference(obj);  

You can't program a weak reference yourself. It's a "special" class that JVM handles specially.

Just use a new weak reference

class MutableWeakReference<T> 

    WeakReference<T> wr;

    void set(T obj)
        wr = new WeakReference(obj);  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文