If you are an Android application developer, the term context must have come across you, as it is one of the most important concepts in Android application development as you can find it wherever you go! In this article, we try to understand what a context is and how to use the right context in the right place to avoid memory leaks!
What is a context?
As the name indicates, the context is the current context or state of the application, as it enables the objects to know some information about the application, the name of the package, for example.
The system also helps in determining the application's required resources (located in the res folder), application files, databases, and SharedPreferences files.
There are several types of context. Activity inherits from Context and there is also an ApplicationContext. Therefore, it is better to know the correct use of each type of Contexts, because the wrong use of context may lead to what is known as a memory leak! But what is memory leak ?!
The concept of memory leak:
To understand the problem, let's make sure we understand how to create and access Objects in Java. The object is created in Java using the keyword new. A reference to the object is returned to us after its creation so that we can access it.
Let's say we have a class called Student:
Student s;
s = new Student("Mohammad");
In the first line, we created a Student reference that enables us to access the Student object we're assigning it to, but in the first line we didn't assign it any value and its initial value is null .
In the second line, we create the Object using new and set the s reference to it. Now what if we want to access the object from another place, or pass it to a specific function:
Student s2 = s;
s.setName("MOHAMMAD");
printStudent(s2);
In the previous code we created a new reference of the Student type and made it refer to the object that the s refer to, so we can now access and modify the object from s, s2 as they refer to the same object in memory. Therefore, passing s or s2 to printStudent will have the same output.
The Garbage Collection:
What if we change the values of s, s2 to point to any other object or null :
s = new Student("Ahmad");
s2 = null;
How can we reach the old object (Mohammad) ?! We have lost the object in memory and cannot access it!
Will he remain so lost in the memory until the program closes!
Of course not, there are those who are responsible for taking over this matter, the object after we lost it has become garbage useless, so we need to clean the memory from it with a process called Garbage Collection, which is a process that the GarbageCollector performs to free the memory from the Unreferenced objects, i.e. the objects that have no reference nor Can be accessed. A dry virtual machine (JVM) performs this process periodically whenever the need arises.
We go back to our problem, the Memory Leak, this problem occurs when we keep in memory objects that we do not need them, and Garbage Collector cannot release them because there is a refernece / s pointing at them!
We will understand the problem further by applying it to Contexts and their types.
Application Context:
It is a single context for the application that supports the Singleton Design Pattern and can be found in the Activity by calling getApplicationContext (). This context is related to the app's life cycle, so you can use it when you want a separate context that is not linked to the current activity.
Usage examples: It can be used when you have a single Singleton Object for the app and needs a Context. Such as objects for accessing databases, fetching data from the server ..
But what if we used the Activity Type Context in these cases ?!
This leads to the Memory Leak, as if the activity is closed, we cannot delete it from the memory in the Garbage Collection process because we have given its refernece to these objects (in the above mentioned cases) that will work throughout the life of the application!
Note: If you are creating a ContentProvider and request getContext () then the object that will return is of type ApplicationContext
Activity Context:
This context is related to the Activity Life Cycle and should only be used if you want to pass the Context to an object related only to the Activity.
Note: ApplicationContext may not be sufficient at times, especially when it comes to UI issues.
Conclusion:
Use the context available to you directly in any component you are in (Activity, Service ...), except in the event that the object you are passing the Context to is not related to the life cycle of the component that you passed. If so, use ApplicationContext.
read also :
Comments
Post a Comment