Home » Java » Different behavior between debug and normal execution mode – WeakReference handling

Different behavior between debug and normal execution mode – WeakReference handling

Posted by: admin July 23, 2018 Leave a comment

Questions:

I patched the java.lang.ref.Reference class to call a custom native method whenever get() is called. The patched class is prepended to the bootstrap classpath.

When I start a sample program I see lot of print outs coming from my native method (as expected) as there are lot of References used internally by the JDK.

The strange behavior starts in my main method. All I’m doing is creating a WeakReference to an object, deleting the strong one and calling get(). For whatever reason the native method I added does not seem to be called in run mode and I do not get any print outs nor other events that are happening inside the native.

If I start the program in debug mode everything works as expected, i.e. the native method is called.

If I change the WeakReference to a SoftReference it always works, also in normal run mode.

I even tried adding other code (like a System.out.println to the get()) but that also did not work. The printing somehow stops for my WeakReference when running in non-debug mode. In debug it always works.

Sometimes I even get a lot of events / print outs from Finalizers after my main, during shut down. So it really seems like the Reference class somehow behaves differently.

Answers: