开发者

out of memory in thread repeatition?

friends,

i am using following code to display single full screen image in activity using thread problem scenario is from custom image gallery on each thumbnail clicking displays large image on this screen.

Now the problem is the user clicks on image and thread loads image and presses back button to go to previous page user keeps clicking each thumbnail one by one to display full screen image and repeats this scenario.

Finally, application crashes with out of memory error bitmap. Please guide what mistake am i doing?

public void Move(final String path)
{
    if (!isConnected()) {
        Constants.DisplayMessage(getApplicationContext(),
            Constants.CONNECTION_ERROR_MESSAGE);
        return;
    }

    progressBar = (ProgressBar) findViewById(R.id.progressbar_default);
    progressBar.setVisibility(View.VISIBLE);

    myThread = new Thread(new Runnable() {
      public void run() {
        while (serviceData == null) {
            s开发者_如何学JAVAerviceData = DisplayLiveImage(path);
            callComplete = true;
        }

        mHandler.post(MoveNow());
      }
    });

    if(myThread.getState() == Thread.State.NEW)
      myThread.start();
}


private  Runnable MoveNow() {
    return new Runnable() {
        public void run() {
            if (callComplete) {
                try
                {
                    if (!serviceData.equals(""))
                    {
                        bm = (Bitmap)serviceData;

                        float ImageHeight = bm.getHeight();
                        float ImageWidth = bm.getWidth();

                        float totalHeight = (ImageHeight / ImageWidth ) * CurrentScreenWidth;

                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                        params.width = LayoutParams.FILL_PARENT;
                        params.height = (int)totalHeight;
                        img.setLayoutParams(params);
                        img.setImageBitmap(bm);
                    }
                    else
                    {
                      // show error
                    }
                }catch(Exception ex)
                {
                    // show error
                }finally
                {
                    progressBar.setVisibility(View.GONE);
                    serviceData = null;
                    callComplete = false;
                }
            }
        }
    };
}

public void stopThread()
{
    try
    {
        if(myThread != null)
          myThread.interrupt();
    }catch(Exception ex)
    { }
}

@Override
public void onDestroy()
{
    if(bm != null)
    {
        bm.recycle();
        bm = null;
    }

    stopThread();
    super.onDestroy();
}


Bitmaps are the most annoying thing to manage with Android.

To avoid most of the OOM exception I use :

//Try to free up some memory
System.gc();
System.runFinalization();
System.gc();

Yes you read right 2 system.gc...Go figure :s

Just before trying to decode it. So in your case it would be before the setImageBitmap I presume. But look into your logcats and put it just before the line that makes your app crashes.

If it still happen try to open a smaller version of your bitmap by using bitmapOtions.insamplesize when you allocate your bitmap.


Are you running the debugger when the out of memory error occurs? If so, then the debugger might be the source of the problem. If you run the app under the debugger, then any threads created will still be retained by the debugger, even when they're finished running. This leads to memory errors that won't occur when the app is running without the debugger. Try running without the debugger to see if you still get the memory leak.

http://code.google.com/p/android/issues/detail?id=7979

https://android.googlesource.com/platform/dalvik/+/master/docs/debugger.html

If this isn't the case, then try using the Eclipse Memory Analyzer to track down what the memory issue might be. http://www.eclipse.org/mat/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜