简体   繁体   English

如何在运行时查找堆/堆栈中的对象有多少个强引用

[英]How to find how many strong references for a object in heap/stack at runtime

Team, 球队,

Is there possible in java, to know how many active/strong references for a object currently available ? Java是否有可能知道当前可用对象有多少个活动/强引用?

For example in the below code; 例如下面的代码; Object of class A can be hold by many classes in the project. 项目中的许多类可以容纳A类的对象。 But i want to print that in the monitor thread. 但是我想在监视器线程中打印出来。

public class A {
  public static A a = new A();
  public static A getInstance() {
     return a;
  }

  private A() {
     new Monitor(this).start();
  }


  class Monitor extends Thread {
      A refA;
      public Monitor(A ref) {
         this.refA = ref;
      }
      public void run () {

      //TODO Print how many references currently available for Object A referenced by refA;
      //Sure It will be minimum one. (which is "a" in this class A)   
      } 

   }
}

Please don't give much importance to this example program. 请不要太重视这个示例程序。 My question is how to find how many strong references available to an object in the heap/stack? 我的问题是如何找到堆/堆栈中的一个对象有多少个强引用? Only good thing is we have one strong reference in hand for that object. 唯一好的事情是我们为该对象有一个强大的参考。

If it is not possible in java; 如果在Java中是不可能的; can i pass this strong reference to C language; 我可以通过对C语言的强烈引用吗? and from C language can i able to do that? 从C语言可以做到吗?

I just wonder how the Profilers/tools are able to do this? 我只是想知道Profilers /工具如何做到这一点? Please help. 请帮忙。

No you can't get the exact count without changing the class or branch tools on the VM (which can hardly be made in production due to the impact on performances). 不,如果不更改VM上的类或分支工具,您将无法获得确切的数量(由于对性能的影响,这种工具很难在生产中进行生产)。

Using the ref package , you can be notified if an object is about to be garbaged (and act at this time) but there is no count available (and not always one handled by the VM). 使用ref包 ,可以通知您对象是否即将被垃圾回收(并在此时执行操作),但是没有可用的计数(并且VM并不总是处理一个)。

You can perform a heap dump and analyse it to find the number of references to any object. 您可以执行堆转储并对其进行分析,以找到对任何对象的引用数。

What is your requirement for doing this and what will you do with the information as I suspect there is an easier/better way to achieve what you want. 您对此有什么要求,您将如何处理这些信息,因为我怀疑有一种更轻松/更好的方法来实现您想要的。


Based on WeakHashMap 基于WeakHashMap

/**
 * Reference queue for cleared WeakEntries
 */
private final ReferenceQueue<Connection> queue = new ReferenceQueue<>();

List<WeakReference<Connection>> usedConnections = ....

// when you have a new connection

Connection connection = ....
usedConnections.add(new WeakReference(connection, queue));

// checking the queue for discarded objects.

    // remove null references from usedConnections

    for (Connection x; (x = queue.poll()) != null; ) {
        synchronized (queue) {
           x.close();
        }
     }

You may try out somthing like this 您可以尝试这样的事情

Class A
{
   static int instanceCount = 0;
   public A()
   {
      instanceCount++;
   }

   protected finalize()
   {
      instanceCount--;
   }

   public static int getInstanceCount()
   {
       return instanceCount;
   }
}

I believe this is the closest you can get to conting references of a class using code. 我相信这是最接近使用代码结束类引用的方法。 Hope it helps ... 希望能帮助到你 ...

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

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