1

Just a simple question, Can a garbage collection happen while a method is being executed? As I believe a piece of code is worth a thousand words so here we go:

 public void start() {
    // view is an instance of WeakReference<View> and is not null
    if (view.get() != null) {
        fetchFeaturedProducts(view.get().getCategory());

        // Some work
        // More work
        // And more work
        //
        // Garbage collections happens, now view.get() is null
        //
        // Is it possible?
        // If yes, then I think there is now way to get around it other then checking right before dereferencing?
        // Or am I wrong and being paranoid?
        // Or is there a sophisticated way to resolve this issue other than using Kotlin?

        fetchProducts(view.get().getCategory(), Manufacturer.All_COMPANIES, Tarteeb.TARTEEB_NONE, true);
    }
}

Really need your expert advices. Thank you :)

Edited: In case that this is possible, I am thinking of creating a dummy class that extends WeakReference and overriding get method to check for null there. And if it is null, return a dummy object of View that does nothing.. But I get a feeling that this is more of a hack than a solution. Any better ideas?

Muhammad Muzammil
  • 1,313
  • 1
  • 12
  • 28
  • No sir it isn't duplicate.. I'm not asking for a definition of Soft or Weak reference. I'm asking for a solution in Java to avoid so many null checks. Please review @Sotirios – Muhammad Muzammil Jul 03 '18 at 06:41
  • 1
    I've removed that one. Refresh for the appropriate duplicate. GC can occur at any time before a call to `get()` and clear the reference. As the duplicate suggests, you'll need to store the result of `get()` and compare it against `null`. You can't directly compare `wr.get() != null` and then call `get()` again because the window between the two might involve a GC that clears the reference. – Sotirios Delimanolis Jul 03 '18 at 06:44
  • Yeah got it.. Thank you – Muhammad Muzammil Jul 03 '18 at 06:46
  • Since you are still using the view store the `view.get()` in a local variable and it won't get collected while you hold the reference. – Peter Lawrey Jul 03 '18 at 07:08

1 Answers1

2

It absolutely can and actually must happen — if there is no method executed the JVM would terminate. Of course the details depend on your actual JVM vendor, operating system and hardware but collections can be triggered while allocations are going on or concurrently. It can stop methods or let them partially continue. You have no control over this (and seldomly need to care).

In addition to that reference processing might happen independent from all of that, so the references can become null in all cases.

The way to deal with references becoming null (and actually works well for normal getters as well) is to assign first to a local variable and then check that variable for null. It well never change under your eyes and it will keep referenced objects alive for the lifetime of your scope (with some restrictions if you do not use the variables).

eckes
  • 10,103
  • 1
  • 59
  • 71