简体   繁体   English

对Java对象的引用数

[英]Number of references to the Java object

Is there some way to check number of references to the Java object? 有什么方法可以检查对Java对象的引用数量?

Let myObject be a reference to an object and the object has already done it's work. 假设myObject是对对象的引用,并且该对象已经完成了它的工作。 I prepare it to die in a disposing method and am going to use the reference myObject for some other purposes, so garbage collector is welcome to kill it. 我准备用一种处置方法将其消亡,并打算将引用myObject用于其他目的,因此欢迎垃圾收集器将其杀死。 But I'm not sure that there are no other references to the object. 但是我不确定是否没有对该对象的其他引用。 How to check it? 如何检查?

As has been stated in the comments, you almost certainly don't want/need to know the number of references to an object. 正如评论中所述,您几乎可以肯定不希望/不需要知道对一个对象的引用数量。 The advantage of garbage collection is that one part of your program can null out its references to an object without caring whether another part of the program still holds references to the object. 垃圾收集的优点在于,程序的一部分可以使对对象的引用无效,而无需关心程序的另一部分是否仍然保留对对象的引用。

If you want to know when an object has been garbage collected, then hold a WeakReference to it as Elliott Frisch said in the comments - if reference.get() returns null then the object has been garbage collected. 如果您想知道何时对对象进行了垃圾收集,请按照Elliott Frisch在注释中的reference.get()持有一个WeakReference-如果reference.get()返回null,则说明对象已被垃圾收集。 A coarser alternative is to override the object's finalize method and log when it's called. 较粗略的选择是重写对象的finalize方法并在调用它时记录日志。

If you think you've got a memory leak then use the Eclipse memory analyzer or some other memory analysis/profiling tool. 如果您认为内存泄漏,请使用Eclipse内存分析器或其他一些内存分析/概要分析工具。

If you really need to know the number of references held to an object then the only solution I can think of is to use the Jikes RVM - it's got a memory management toolkit that includes reference counting collectors with backup cycle collection, you should be able to hook into the collector to determine the number of references held to an object (after you run the backup cycle collector, unless you're certain that the object doesn't hold a transitive reference to itself). 如果您真的需要知道保存到一个对象的引用数,那么我能想到的唯一解决方案是使用Jikes RVM-它具有一个内存管理工具箱,其中包括带有备份周期收集的引用计数收集器,您应该能够挂接到收集器,以确定持有给对象的引用数(在运行备份周期收集器之后,除非您确定该对象没有对其自身的传递引用)。 Using the RVM is not a trivial task; 使用RVM并非易事。 also, you should not use the RVM in a production environment - it is strictly a research tool. 同样,您不应该在生产环境中使用RVM-严格来说,RVM是一种研究工具。

You could also try hacking together something like the following 您也可以尝试一起破解以下内容

public class Wrapper<T> {
  private WeakReference<T> thisReference;
  private ConcurrentLinkedQueue<WeakReference<Object>> references = 
    new ConcurrentLinkedQueue<>();

  public T getObject(Object obj) {
    T thisObject = thisReference.get();
    if(thisObject != null) {
      references.offer(new WeakReference(obj));
    }
    return thisObject;
  }

  public int referenceCount() {
    if(thisReference.get() == null) {
      references.clear();
      return 0;
    } else {
      Iterator<WeakReference<Object>> iterator = references.iterator();
      while(iterator.hasNext()) {
        WeakReference<Object> reference = iterator.next();
        if(reference.get() == null) {
          iterator.remove(); 
        }
      }
      return references.size();
    }
  }
}

The idea is to force objects to acquire a reference to the object via getObject , which registers a WeakReference to the acquiring object. 这个想法是通过getObject强制对象获取对对象的引用,该对象将WeakReference注册到获取对象。 referenceCount will then count the number of these WeakReferences that are non-null. 然后, referenceCount将计算这些非null的WeakReferences的数量。 This is simpler but not as accurate as using a true reference counting collector via Jikes, because there may be non-reachable WeakReferences in the references collection that haven't been garbage-collected yet and are therefore non-null; 这比通过Jikes使用真正的引用计数收集器更简单,但不那么准确,因为在references集合中可能存在无法到达的WeakReferences ,这些references尚未被垃圾回收,因此为非null。 there's also nothing preventing the escaped thisObject from being passed to another object that won't have been registered via getObject . 也没有什么可以防止将转义的thisObject传递给另一个不会通过getObject注册的对象。 This also introduces the new problem of who's holding a reference to Wrapper , so I'm not sure if this would gain you anything. 这也引入了一个新问题,即谁拥有对Wrapper的引用,因此我不确定这是否会为您带来任何好处。 The bottom line is that accurate reference counting would need to be done at the level of the virtual machine (ie Jikes), not user code. 最重要的是,需要在虚拟机(即Jikes)级别而非用户代码级别进行准确的引用计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM