I'd like to know if there is a way to check how many references a Java object has. As far as I could check the only way to do that is using JVMTI through a JNI interface. Is there a pure Java (without using native libraries) solution to get this information?
We are developing an educational tool for data structure animation(to be used with students implementation of certain algorithms), so it would be very nice if we could check for "released" objects on the most non-intrusive way (I'm trying to avoid forcing the user of this tool to call a method such as objectReleased(objRef)
in order to update the data structure animation for an element removal or something similar).
Every object in Python has a reference count and a pointer to a type. We can get the current reference count of an object with the sys module. You can use sys. getrefcount(object), but keep in mind that passing in the object to getrefcount() increases the reference count by 1.
You can always check the reference count of a variable by using the CFGetRetainCount function. Note: Reference counting is done only to manage memory for reference types (e.g., classes and closures).
A Formula that references a field on another object is known as a Spanning Relationship. The limit of spanning relationships per object is 15. This means that an object can only contain up to 15 different object references.
Java doesn't offer this option natively as far as I know.
Here you have some guidance on how to do it manually:
http://www.velocityreviews.com/forums/t363649-how-do-i-get-a-reference-count-of-a-object-in-java.html
From your description, it seems you care less about the actual count of references than to simply know when an object has been collected. If this is the case, you can use WeakReference
or PhantomReference
to determine when a referenced object is ready for finalization.
See:
WeakReference
: http://download.oracle.com/javase/6/docs/api/java/lang/ref/WeakReference.html PhantomReference
: http://download.oracle.com/javase/6/docs/api/java/lang/ref/PhantomReference.html If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With