I want to make sure that the reference remains.

Asked 2 years ago, Updated 2 years ago, 130 views

Please tell me about Android's Context.

I have seen blogs that recommend getApplicationContext() instead of using this to pass as arguments.

If you use this, you should use getApplicationContext() because it may cause memory leaks if you use this to free up memory.

So I have a question, is there a way to check the behavior of remaining references?

android java android-studio

2022-09-30 20:24

2 Answers

First of all, it is not always right to use getApplicationContext() instead of this in situations where Context is required.I think it's the same as the blog I saw before, but I've seen many examples of using the wrong Context just because I saw the blog.

Specifically, if used for operations involving View, for example, if this is Activity, create View that reflects the theme information set to that Activity.

As for whether the references in the question remain, it is up to GC's results to decide whether or not they will be open because it is Java.Therefore, the expression "even if you release memory" is not correct (you cannot explicitly open it), but I think it is easy to use WeakReference in the sense that all references on the code have been cleared.

When using Context, create a ref as WeakReference ref=newWeakReference(this); and keep it somewhere other than this.If this appears to be open, try System.gc() and then ref.get(), and if it is null, it means that there was no code referencing the instance anywhere.

There are other ways to check with debugging tools, but if necessary, please ask me again.


2022-09-30 20:24

@zaki50's way of thinking is the same, but it's a different solution.

In @zaki50's way, ref.get() returns the entity of the reference (i.e.Context instance), which may interfere with GC depending on how you write the code.You may also need to run ref.get() many times to wait for GC.

You can use ReferenceQueue to avoid them.If you register WeakReference with this, you can receive notifications through ReferenceQueue when there is no reference to that reference.

First, when using the Context instance, register it in the ReferenceQueue as follows:

ReferenceQueue<Context>queue=newReferenceQueue<>();
WeakReference ref = new WeakReference <>(this,queue);

Keep these queue and ref somewhere you can refer to later and not GC.
Then, when you think that the reference to the registered instance is gone, you can do the following to verify that it really is gone:

System.gc();
queue.remove();
System.out.println("No longer referenced";

When the garbage collector notices that there is no reference to the registered instance, the garbage collector thrusts the WeakReference of the instance into the queue.
queue.remove() blocks queue until weakReference is stuck in queue, so you can wait for GC without looping.
queue.remove() does not return the registered instance itself, but its WeakReference, so it does not interfere with GC at all.


2022-09-30 20:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.