0

I need to pre load a complex layout so I can show the activity more quickly the first time:

LayoutInflater inflater = (LayoutInflater) mainActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

SlowActivity.cachedView = inflater.inflate(R.layout.activity_layout, null, false);

when SlowActivity starts...

public static View cachedView = null; 

@Override
public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
        setContentView(this.cachedView);
      }
}

I can't find where I read it but some one says that I'm doing a memory leak using a stati variable to store inflated layout.

Why?

Maybe I need to release some resource when activity is destroyed (never, it's always put on the background...)

Seraphim's
  • 12,559
  • 20
  • 88
  • 129

2 Answers2

3

that's because the layout , just like all views , has a reference to the activity that holds it.

so, after the activity was closed and should be releaseed , the static variable holds a reference to a view that references to the this activity, so the app takes more memory than it should .

views are not the only variables that you should try to avoid caching using static variables . an example for this is drawables , as seen here .

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Fortunatelly I used static variables only in this just to speed up the comple layout loading (1-2 seconds). Can I force the GC to free memory occupied by the static variable? – Seraphim's Jul 31 '12 at 12:42
  • 1
    you can't force , you can give it a clue that now it's a good time for it , using System.gc() . of course , the minimal thing to avoid memory leak would be to set null to the reference . you can also use weakReference or softReference if you aren't sure when to set null , but you need to be careful of that . – android developer Jul 31 '12 at 12:59
  • Ok, it's enough for me, I need more knowledge on "memory leak" before continue. Thank you. – Seraphim's Jul 31 '12 at 14:20
  • 1
    you can watch this video too: http://www.google.com/events/io/2011/sessions/memory-management-for-android-apps.html , and do note that in any case , caching views is can be problematic (especially for all classes that extend adapterView,look at the "the world of listView" video for more information ) , so if you choose to do it , do it carefully . – android developer Jul 31 '12 at 14:24
1

Because Android can ( and will ) destroy your Application/Activitys ( including static variables! ) everytime it needs memory. So you have to think about it, when it is time to create the static variables. Or else you will get weird and hard to find crashes after the application was inactive.

fklappan
  • 3,259
  • 2
  • 17
  • 18
  • So I can manage to load layout normally if I found that static variable is null: `if (cachedView != null) { setContentView(cachedView); } else { setContentView(R.layout.activity_layout); }` – Seraphim's Jul 31 '12 at 11:02
  • ...but how can I force GC to free the static layout variable? – Seraphim's Jul 31 '12 at 12:25
  • Sorry im not as much into java to tell you deeper insights of the GC. But your solution should work. I did it a similar way – fklappan Jul 31 '12 at 12:45