2

I have two situations:

  1. When an object (has only strong refs) loses all of its strong references, it becomes available for garbage collection.
  2. When the object has only weak references, it also becomes available for garbage collection.

In what situation the object will be collected faster? Or there is no difference?

I'm working on the old android application. And my predecessor used the weak reference (as instance variables) to storing views in the holder for the RecyclerView's adapter. I want to know why he did that. I had an idea that can be weak reference forced GC to collect the object. I mean that in the next garbage collection the object with only weak references to 100% will be collected when object without references maybe not. This can be so?

  • It is not quite clear what do you mean by situation #2 – Andremoniy Feb 01 '17 at 07:16
  • Can you even create weak references in Java? – Sweeper Feb 01 '17 at 07:23
  • @Sweeper Sure. [Soft references](https://docs.oracle.com/javase/8/docs/api/java/lang/ref/SoftReference.html) and [phantom references](https://docs.oracle.com/javase/8/docs/api/java/lang/ref/PhantomReference.html) too. – Kayaman Feb 01 '17 at 07:26

1 Answers1

3

Why would that matter?

The point is: the garbage collector starts collecting eligible objects ... when it "thinks" it is required to do that. In other words: it doesn't matter when your objects become eligible; what matters is that they are eligible when the GC starts collecting.

Beyond that: you can't distinguish your two cases in many cases.

Meaning: if you use a weak reference for X; that still means that other "strong" references might exist. The object only becomes eligible when those strong references go away.

The key point is: only eligible objects will be collected. Their history that leads to make them eligible does not matter at all.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I'm working on the old android application. And my predecessor used the weak reference (as instance variables) to storing views in the holder for the RecyclerView's adapter. I want to know why he did that. I had an idea that can be weak reference forced GC to collect the object immediately after the loss of strong references. This can be so? – Владимир Широков Feb 01 '17 at 08:27
  • Not to my knowledge. As I said: the GC does its job when it thinks "it is time to collect". For sure it doesn't start collecting just because one object lost the last strong reference pointing to it. The point of weak references has *nothing to do* with "when GC takes" places. It is all about memory leaks; and allowing objects to "gently" vanish. – GhostCat Feb 01 '17 at 08:31
  • Ok. I am stupid) Not "immediately ". I mean that in the next garbage collection the object with only weak references to 100% will be collected when object without references maybe not. – Владимир Широков Feb 01 '17 at 08:44
  • "I want to know why he did that." your question should reflect that, since that question may be more easily answered than your guess at why he might have done that. – the8472 Feb 01 '17 at 08:46
  • I updated my answer. The point is: *when* an object is eligible, then it will be collected. And it doesn't matter at all what happened before and *how exactly* that object became eligible! – GhostCat Feb 01 '17 at 08:51
  • But only weakly reachable object is not eligible. [Java doc](https://docs.oracle.com/javase/7/docs/api/java/lang/ref/package-summary.html#reachability) **Reachability** "An object is weakly reachable if it is neither strongly nor softly reachable but can be reached by traversing a weak reference. When the weak references to a weakly-reachable object are cleared, the object becomes eligible for finalization." – Владимир Широков Feb 01 '17 at 09:33
  • You are not reading things correctly. The **whole** point of a using weak references is to **allow** their targets to be collected. See https://docs.oracle.com/javase/7/docs/api/java/lang/ref/WeakReference.html for example. – GhostCat Feb 01 '17 at 09:36
  • Ok. I understood. Thank you for your patience! – Владимир Широков Feb 01 '17 at 09:48