All references to an object

前端 未结 3 1506
遇见更好的自我
遇见更好的自我 2021-01-22 14:03


Is it possible to obtain all references to an object in Java.

What I need to check is if an object has removed all subscriptions for callbacks.

Thanks

相关标签:
3条回答
  • 2021-01-22 14:30

    This is possivble via JVMTI and typically done by heap profilers. However, it cannot be done from within Java.

    0 讨论(0)
  • 2021-01-22 14:34

    this is not possible from within the JVM but you can create a heap dump by using jmap from the jdk, then you can parse the dump file using jhat and click through the references which were alive when the dump was taken.

    checkout this blogpost from frank kieviet, where jmap and jhat are used to identify permgen leaks in tomcat: http://blogs.oracle.com/fkieviet/entry/how_to_fix_the_dreaded

    and the jmap and jhat sites:

    http://download.oracle.com/javase/6/docs/technotes/tools/share/jhat.html http://download.oracle.com/javase/6/docs/technotes/tools/share/jmap.html

    0 讨论(0)
  • 2021-01-22 14:38

    One approach would be to keep a list of weak references to all objects to which subscriptions have been requested. Whenever the object subscribes to another, add to the list a weak reference to the other. Whenever the object unsubscribes, remove the weak reference. Verify that all subscriptions have been removed by checking that the list is empty.

    If necessary, this can be generalized for objects that support multiple types of subscriptions.

    A false positive can occur if a subscription is not accompanied by adding to the list.

    0 讨论(0)
提交回复
热议问题