Android memory leak between actions

I am trying to detect this memory leak.

I have two SurfaceViews, Aand B. I started A, then go to B, then click the back button to go back to A, and then go to again B.

I can see that my allocated memory is increasing every time I do this, and in the end I will get an error from memory.

This is how I navigate to B, from the inside SurfaceView, connected toA

        Context context =  this.getContext();
        Intent i =new Intent(context, StartCareer.class);
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(i);

In both views, I have many bitmap drawings. As BI can not find a reference to A, and the only reference outside of the context of which I can think of is a reference to a class Global, which I have. I also have some analytics that happen in the background. It could be a million different things, I would suggest that

I have a DDMS view on Eclipse, but I'm not sure what I'm looking for, or how to find an exact object that keeps repeating itself.

I would take either a course / course on the DDSM Allocation Tracker, or someone to indicate what I am doing wrong.


Additional Information:

I have several bitmaps drawn on SurfaceView. Examples of these Bare:

////At class level
Bitmap rightB,leftB;
////In the constructor
rightB = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.right), 100,75, true);
////In doDraw
canvas.drawBitmap(rightB, rbX, rbY, null);

And my onDestroys

@Override
public void surfaceDestroyed(SurfaceHolder holder) {


    if (mThread.isAlive()){
        mThread.setMenuRunning(false);
    }
}

, MAT , , . Thread . .

@Override
public void surfaceCreated(SurfaceHolder holder) {
    loading=false;
    if (!mThread.isAlive()){
        mThread = new ViewThread(this);
        mThread.setMenuRunning(true);
        mThread.start();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

    if (mThread.isAlive()){ 
        mThread.setMenuRunning(false);
    }
}

, , , . , ?

+5
5

DDMS (*.hprof ), "".

*.hprof sdk "hprof-conv.exe"

eclipse (http://www.eclipse.org/mat/) ( ) *.hprof file, "memory leak analyzer" , , , ( , bufferObjectManager 50% ).

(BufferObjectManager.getActiveInstance().unloadBufferObject(((RectangularShape) obj).getVertexBuffer());), , .

, .. , (onDestroy();) .

+3

onDestroy() onstop() .

private void unbindDrawables(View view) {
     Log.d(TAG,"in unbindDrawables");
        if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
        }
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
            }
        ((ViewGroup) view).removeAllViews();
        view.setBackgroundResource(0);
        Log.d(TAG,"removed views");
        //finish();
        }
 }
+4

:

  • Recycling bitmaps when executed with activity (e.g. onDestroy)
  • Use the application context, not the process itself, as context when possible.
+3
source

Try recycling () your bitmaps into onDestroy () of your actions.

+1
source

What you need to do is described in detail here . For your specific problem you need to do this.

// resize to desired dimensions
    int height = b.getHeight();
    int width = b.getWidth();
    Log.d(TAG, "1th scale operation dimenions - width: " + width + ",
       height: " + height);

    double y = Math.sqrt(IMAGE_MAX_SIZE
            / (((double) width) / height));
    double x = (y / height) * width;

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, (int) x, 
       (int) y, true);
    b.recycle();
0
source

All Articles