1

In my project it keeps crashing due to the VM budget being exceed. The pictures that I am using are all very small but supposedly they keep filling up the VM. I was working through eclipse and notice 2 instances where it gave me the following errors.

This Handler class should be static or leaks might occur (com.quickreaction.BT_screen_menuButtons.2) BT_screen_menuButtons.java /BT_activity_root/src/com/quickreaction line 1091 Android Lint Problem

When I follow the two links this is the source code that it brings me too.

Handler downloadScreenDataHandler = new Handler(){ 
@Override public void handleMessage(Message msg){ 
if(JSONData.length() < 1){ 
hideProgress(); 
showAlert(getString(R.string.errorTitle), getString(R.string.errorDownloadingData)); 
}else{ 
parseScreenData(JSONData); 
} 
} 
};  

And..

private Handler buttonImageHandler = new Handler() { 
public void handleMessage(Message msg){ 
//BT_debugger.showIt(activityName + ":buttonImageHandler setting background image for        button."); 
//msg.what will equal the index of the button images array... 

//set the drawable... 
Drawable d; 

//we may need to round the image... 
if(buttonCornerRadius > 0){ 
d = buttonImages.get(msg.what); 

//we have a drawable, our rounding method needs a bitmap... 
Bitmap b = ((BitmapDrawable)d).getBitmap(); 
b = BT_viewUtilities.getRoundedImage(b, buttonCornerRadius); 

//convert it back to a drawable... 
d = new BitmapDrawable(b); 

}else{ 
d = buttonImages.get(msg.what); 
} 
buttonSquares.get(msg.what).setBackgroundDrawable(d); 
buttonSquares.get(msg.what).invalidate(); 

} };

I have been reading on stack overflow about making the handler static or weak but don't know how. Any ideas

2 Answers2

1

These lint messages about making Handler weak or static can usually be ignored. If you are creating a Handler and storing a reference to it in your activity, then when your activity is destroyed the Handler will also go away. There is no leak here. The only time there could be a leak is if, when your activity goes away, there is still a message in the message queue for that Handler. However, this is usually not the case.

After looking at your code, I conclude that you can't make your Handler static (ie: and inner class) because it needs a reference to its outer class (the activity). Also, making it a stand-alone class (like in CommonsWare's answer) won't help either because you will need to pass a reference to your activity when you instantiate the stand-alone class, so this won't help solve the "leak" either (if there really is one). It might get the stupid LINT warning to go away though :-)

If you are having memory issues, you should use a heap analyzer like JHAT or MAT and actually see what objects you've got in memory before paying attention to these silly lint warnings.

See also This Handler class should be static or leaks might occur: final Handler and This Handler class should be static or leaks might occur: IncomingHandler

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

Instead of using an instance of an anonymous inner class (e.g., new Handler() {}), create a static inner class (e.g., static class MyHandler extends Handler) or a regular Java class (class MyHandler extends Handler) and use it instead.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491