开发者

Android application crashes due to low memory

My application is basically a image viewer. It is opened from both camera and as a separate application开发者_StackOverflow中文版.

I open the image viewer to view and edit the picture. Each edit operation is implemented using thread. If my application closes due to pressing the home button, the next time I open it with camera. It throws anr.

This doesn't always happen. Only when large edit operations or edit operations on large image files are done.

I get out of memory error, sometimes timeout.

I guess it s because the thread doesn't complete the edit operation when home is clicked. and it s still running on the background. so when i open it s unable to process it.

m I right?

If so what is the way to stop a thread before the completion/

Can u pls help me out?


Looks like memory leak. Check for memory leaks in your code


Might look at a thread I opened on out of memory issue on large images. Several suggestions in there on what might help you: Strange out of memory issue while loading an image to a Bitmap object


Threads are not garbage collected until they are stopped or their process is killed (which usually takes a while, even after all activities are closed). That also means that any objects referenced by those threads aren't garbage collected either.

There are multiple ways to shut down a thread, depending on what exactly it is doing. Best way is to check for a variable at certain points during it's execution, and exit the run-Method when that variable is set. Best place for that is usually within the condition of a while-loop. You can than set that variable from the outside, and the thread will shut down the next time it checks for it. Remember to mark that variable as volatile. Example:

private volatile boolean mStopped = false;

public void run() {
   while(!mStopped) {
      // do something
      if(mStopped)
          return;
      // do something more
   }
}

If a thread is waiting, you'll have to interrupt it. See the interrupt-Method of the Thread-Class for that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜