Punit Pandey
Punit Pandey

Reputation: 405

Android Memory Leak and Garbage Collection

I have a fragment which keeps reference to the parent activity. On onCreateView method I initialise the adapter by passing a static list to the adapter. As the list is "static", does it mean that the Activity, Fragment and Adapter will never be garbage collected?

Here is my code -

public class MyFragment extends Fragment 
{
RecyclerView rvMyContestLists;
MyContestListAdapter adapter = null;
Activity activity;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
        // CConstantVariables.listMyContestData is static
        adapter = new MyContestListAdapter(activity, CConstantVariables.listMyContestData);
        rvMyContestLists.setAdapter(adapter);
        rvMyContestLists.setLayoutManager(new LinearLayoutManager(getActivity()));
 }
 }

Does using "static" variable CConstantVariables.listMyContestData as adapter's list data mean that the Activity will never be Garbage collected? Does this code indicates memory leak?

Upvotes: 1

Views: 1454

Answers (3)

user1460340
user1460340

Reputation:

Static word usually means that your data is still at a certain specific location in memory and that memory location do not changes while your app is running. Let's go bit further, having static value assign a specific location on device RAM and you can access the location anytime from any activity once located variable has been initiated. So as long as your app runs, the static variable once saved may happen never be collected as garbage and the heap will keep on increasing as keep increasing use of static variables. What you can really do is just set the variable null if you want to remove its stored value. (Not location)

What I generally do is recycle them. It's not a code that recycles, but I actually mean that instead creating a lots of static variable, I creates few (in my own app) and keep changing their value as per need, hence controlling useless memory expansion. (Likewise Recycle as we do plastics or any metal)

Yes, static variables may create memory leaks as its once assigned and if not set to null may give a certain location in memory permanently till your app ends. In case of bigger stored values such as bitmaps you can clearly observe the difference between static and normal variable.

Conclusion: Try avoiding statics as much as you can till you are not left with any other choice else than using static. If you do use then keep cleaning the stored memory, you can't remove location from RAM but can set value as null.

Note: Functions used with static variables will be garbage collected normally, like adapter of listview if it is not static. (In your case, you have adapter garbage collected).

Hope I might be able to explain well, or comment if any question or any correction.

Cheers !

Upvotes: 0

ben75
ben75

Reputation: 28746

The GC will collect all objects that aren't referenced from a GC-root object. The GC-root objects are typically:

  • all running threads
  • all static fields

In your sample : CConstantVariables.listMyContestData is static and so a potential source of memory leak. You must keep the content of this list under control:

  • ensure that this list don't contains objects that you don't need anymore
  • ensure that the objects in that list don't hold references to objects that you don't need anymore.

Using a static list in the adapter won't prevent the adapter from being garbage collected (once there is no more references from GC-root objects to the adapter).

I suggest you this very good talk about tracking memory leak in Android : https://www.youtube.com/watch?v=_CruQY55HOk

Upvotes: 1

idailylife
idailylife

Reputation: 184

It seems that your View will still be garbage collected if it meets with the specified life cycle of Fragment. As for the static CConstantVariables, it will remain in the memory until your application ends.

Please refer to Static member views in activity - Android and Using static variables in Android

Upvotes: 0

Related Questions