Junior — Middle
What reasons could lead to the garbage collector not removing the context stored in the application?
sobes.tech AI
Answer from AI
Garbage collector in Android will not delete a context stored in an application if there are still strong references to it. Main reasons:
- Memory leaks caused by static variables. If the context is stored in a static variable, it will live until the process ends.
- Long-lived references from other objects. For example, if the context is passed to an object with a long lifecycle (like a singleton), the collector cannot delete it.
- Internal reference cycles. Although Java GC can handle cycles, if the context is linked to objects that are actively used themselves, it will not be removed.
- Using Activity context instead of Application. Activity context lives only while the activity is active, but if stored in long-lived objects, it can lead to leaks.