0

I read from Romain Guy about avoid memory leak in android. I understand that I have to

use getApplicationContext() to avoid mem leak. Anyway, my code is as follows:

MainMenuActivity.java

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
canvasMenu = new CanvasMainMenu(this);
setContentView(canvasMenu);
}

CanvasMainMenu.java

public class CanvasMainMenu extends View {
       private TextView textChallenge;
       public CanvasMainMenu(Context context) {
              super(context);
                      textChallenge = null;
                      textChallenge = new TextView(context);
                      textChallenge.setTypeface(fontJoan);
                      textChallenge.setTextColor(Color.WHITE);
                      textChallenge.setText("99");
                      textChallenge.measure((int)Define.getScreenWidth(), (int)Define.getScreenHeight());
                      textChallenge.layout(0, 0, 200, 200);
                      textChallenge.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20);
                      textChallenge.setDrawingCacheEnabled(true);
                      textChallenge.buildDrawingCache();
       }
}

My question is, how can I avoid mem leak if my codes look like this.

Do I have to create static TextView in file MainMenu.java?

Someone please explain me if I understand something wrong.

barssala
  • 463
  • 2
  • 6
  • 22

1 Answers1

0

Why would you think this code leaks memory? The only weird thing about it is the use of a private TextView object (textChallenge) inside your CanvasMainMenu. Why do you create this? It doesn't seem to be of any use in your class.

To address your specific question: in general, creating static view references is the source of memory leaks, not a way to avoid them. Don't do it unless you absolutely have to (an extremely rare circumstance) and you know what you are doing.

For your code, I also don't see how using the application context will help with memory leaks. That's generally only useful when you need a context reference that will survive an activity being destroyed and re-created (such as after a device reorientation).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • sorry, I use `textChallenge` in `onDraw()` to draw this text. And when I create a lot of `TextView`. I check alloc size by `(Debug.getNativeHeapAllocatedSize() / 1048576L)` and found that when I come back to `MainMenu.java`. The alloc size will be increased a little bit every time. That's why I thought it was mem leak problem. – barssala Feb 27 '13 at 08:47
  • You said `View` references. What is that? It's something like `View`, `SurfaceView`, `TextView`, `Bitmap`. Am I right? – barssala Feb 27 '13 at 09:21
  • @barssala - Yes, any variable that is of a type derived from `View` is a view reference. Static `View` references are a common source of memory leaks because they maintain a reference to the activity and, being `static`, don't go away when the activity is destroyed. For drawing text in a custom view, you should be using a [`Layout`](http://developer.android.com/reference/android/text/Layout.html) object, not a `TextView`. – Ted Hopp Feb 27 '13 at 16:08
  • @barssala - As far as finding the source of the memory leak (if that's what's happening), take a look at [this thread](http://stackoverflow.com/questions/1147172/what-android-tools-and-methods-work-best-to-find-memory-resource-leaks). – Ted Hopp Feb 27 '13 at 16:10