I've been writing a Java client which uses JDI to create and modify objects in a remote JVM (by connecting to a JDWP agent-based server running in the remote JVM). One of the requirements of my project is that I can't suspend all of the threads in the remote JVM, which means that objects I create can be susceptible to garbage-collection before I can make them reachable within the JVM.
In some cases, I am creating objects in a remote JVM but they are randomly being garbage-collected. For example, if I create an array in the remote JVM via ArrayType.newInstance(int)
, sometimes the array will be garbage collected before I can make it "reachable" from another reachable object in the remote JVM.
(eg If I'm trying to store the new array into a field of an existing reachable object, the call to ObjectReference.setValue(Field, Value)
may randomly throw an ObjectCollectedException
.):
void createAndStoreArray(ObjectReference reachableObj, Field fieldOfObj, ArrayType type, int length)
{
ArrayReference ref = type.newInstance(length);
reachableObj.setValue(fieldOfObj, ref); // Sometimes throws ObjectCollectedException because ref's mirror garbage gets collected before I can store it on the reachable object
}
In theory, a ObjectReference
's mirror could even get garbage-collected before I'm able to call ObjectReference.disableCollection()
(which is a step I don't want to take for other reasons anyway).
So my question is, are any documented lifespan guarantees on JDI Value
s?
- Is a mirror of a primitive value exempt from GC in the remote JVM? (One one assume it would be but none of the
VirtualMachine
. mirror*()
methods documents says anything.) - Is a mirror of a String exempt from GC? (One would think not, but the JavaDoc seems to be silent.)
- I assume any other
ObjectReference
can be GC'd at any time unless you manage to disable GC on it before?
Thanks in advance for your help!