Weak links problem

There are one or two in my program weakreferences

.

For example only:

ClassX myClassX= new ClassX(); //Line 1
WeakReference<ClassX> myWeakClassX = new WeakReference<ClassX>(myClassX); //Line 2
if(myWeakClassX.get() != null) // Line 3
{
    //do something with reference //Line 4
}

      

My question is:

How is it ensured that if line 3 myWeakClassX.get()

has a valid reference to Object

, it is also valid on line 4? I can imagine that if you're really out of luck, the GC is doing its job exactly "between" lines 3 and 4. Please bear with me, because I'm relatively new to Android / Java.

Thanks for any explanation.

+3


source to share


2 answers


In Java, the first thing to understand is the garbage collector, which reclaims memory from objects that are eligible for garbage collection.

Q: how is eligibility determined?

eligibility is determined based on which links link to this object.

Why do we need a weak link?

If you create a Strong reference to an object, the object cannot be garbage collected. Whereas a weak link just supplied is a link that is not strong enough to force the object to stay in memory. Weak references allow you to take advantage of the garbage collector's ability to determine availability for you, so you don't have to do it yourself.

The problem is here

A weak reference is not strong enough to prevent garbage collection, so you may find (if there are no strong references to the class) that myWeakClassX.get () suddenly starts returning null.



What's the other option?

Soft link

You use SoftReference when you want the referenced object to stay alive as long as the host process is not running in memory. The object will not be eligible for collection until the collector wants to free memory. It is unclearly stated that the SoftReference binding means "Hook the object until you can no longer."

This way myWeakClassX.get () will not be empty.

Examples of where can we use?

+2


source


You are correct that during lines 3 and 4 the operation get()

can return null

how it should be done. You can always copy the reference you get from the operation get()

into a variable (thereby making it strong again) and use it safely in a block if

. Since you still have a strong reference, the object will not be garbage collected.

A simple check for null could be:



if(myWeakClassX.get() != null)
{
  ClassX myref = myWeakClassX.get();
  if(myref != null) {
      //use it
  }
}

      

However, in the case of actions, a strong reference does not guarantee that the action will not be destroyed, and although you have a strong reference that is valid, the activity can throw exceptions when you try to use it because it is destroyed, Example. If you keep a reference to the activity inside some AsyncTask, the activity may be destroyed (for example, changing the orientation) before the AsyncTask starts. Although you will have a link to it, you will get exceptions when you try to update the interface. This is where you can create a WeakRefernce for an activity inside an AsyncTask, and if the get () operation starts returning null, you will know that the activity was destroyed for some reason and did not try to use it.

+2


source







All Articles