When it is ok to use strong references in android and does this code leak?

When it is ok to use strong references in android and does this code leak?



It is not clear to me when is best to use WeakReference to avoid memory leaks in android. Example:

Code in fragment:


WeakReference


containerView.setDataForDisplay(customer, new CustomListener()
@Override
public void buttonClicked(@NonNull Customer customer)
if(handler != null)
handler.buttonClickedForCustomer(customer);


);



The code inside the custom LinearLayout


LinearLayout


public void setDataForDisplay(List<Customer> customer, CustomListener listener)
// view setup code

someView.setOnClickListener( v ->
if(listener != null)
listener.buttonClicked(v.getTag());

);



The anonymous class CustomListener has a reference to the containing fragment's this.

That anonymous class is passed as a listener to a containing view which in turn does a callback eventually.


CustomListener


this



My question is does this lead to a memory leak?

Should the listener be hold somehow in a WeakReference? How do we decide when it is ok to have a strong reference vs a weak reference?


listener


WeakReference





well, it's all about dependency and scenario, let's take a common example of async task, which does require a context doing some task in the background, now suppose before the task completion, you closed the host activity. Now in async's background task is over and will look for context but context is not there since host activity we closed it. That kind of scenario where we use a weak ref to hold the data.
– notTdar
Sep 1 at 15:45





@notTdar:That case assumes a static reference to the async task right? Because if it was an instance variable of the activity would there be a problem?
– Jim
Sep 1 at 17:06




2 Answers
2



No, this isn't a memory leak.



A simplified reference graph might look something like this:



references graph



True, the graph does contain a circular reference, but the garbage collector is perfectly capable of detecting & addressing that situation. The entire graph would be GC'd after the system loses its reference to the Fragment, which is generally going to be right around the time it's dismissed from the screen.


Fragment



I can't contrive a situation where a WeakReference would be needed to prevent a memory leak. (I'm not saying they don't exist, I just can't come up with one.)


WeakReference



I see WeakReferences as a tool for advanced memory management tasks, of the kind that rarely pops up in general application development. Like: Managing a collection of large items, where it's OK if some of them get GC'd (as memory runs out), but you'd rather keep them in RAM.


WeakReferences



IOW: Don't keep around any references you don't need, and you should be OK.



Modern JVM has a very powerful garbage collector. It manages to collect even cyclic references, by detecting objects that are completely isolated form gc roots. You can read more on this topic here:
How Garbage Collection Really Works



By using weak references. however, you can speed up the work of the gc, because this references are to be collected immediately after all strong references on this object are gone.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ