3

Is it possible to test for the existence of other references to an object in java?

Even if the operation is inefficient, I would like to assert while debugging that a given object will be garbage collected once a given reference falls out of scope.

Is this possible?

Thanks

Chris
  • 3,702
  • 3
  • 23
  • 15

3 Answers3

2

You can simply override finalize method, then put a breakpoint inside the method. Once the object is garbage collected, you will see in debugger. However please note, that it is highly unwanted to override finalize method in production systems, since it affects badly the GC. So after debugging, remove the method.

Gábor Lipták
  • 9,646
  • 2
  • 59
  • 113
2

There is no standard Java API to do so.

Your particular JVM may, however, be able to by using JVM specific diagnostic API's.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

As Anderson said about the JVM api . The string class uses the intern method to check the object already exists in the pool . They use this API called

   public String intern() {
          return VM.intern(this);
   }

org.apache.harmony.kernel.vm.VM class which is a native code.

Dead Programmer
  • 12,427
  • 23
  • 80
  • 112