Memory Leak issue


Last Updated:

  1. j_p36

    j_p36 Member This Topic's Starter

    Joined:
    May 19, 2010
    Messages:
    16
    Likes Received:
    0
    I am having a memory leak and the solutions I've found when searching the web haven't been much help. I know what the problem is, I just don't know how to fix it. The problem is that I'm allocating images as final (actually stored in a final linearlayout view) and they don't get deallocated. But I'm not sure how to do that because of the way I'm creating them.

    The problem happens on a screen rotation, when the activity is destroyed and recreated.

    Any suggestions are welcome and thanks for your time. My code and error log are below.

    Code (Text):
    1.  
    2. db.open();
    3.         Cursor cursor1 = db.getItemFromInfo("picvid", iid);
    4.         if(cursor1.moveToFirst())
    5.         {
    6.             while(!cursor1.isAfterLast())
    7.             {
    8.                 String uriString = cursor1.getString(2);
    9.                 final Uri uri2 = Uri.parse(uriString);
    10.                 String[] projection = { MediaStore.Images.ImageColumns.DATA, /*col1*/
    11.                                 MediaStore.Images.ImageColumns.DISPLAY_NAME /*col2*/};
    12.  
    13.                 Cursor c = managedQuery(uri2, projection, null, null, null);
    14.                 if (c!=null && c.moveToFirst()) {
    15.                         name = c.getString(1);
    16.                         Log.d("Display name",name);
    17.                 }
    18.                 c.close();
    19.                 if(!name.equals(""))
    20.                 {
    21.                     if(name.toLowerCase().contains(".jpg"))
    22.                     {
    23.                         final LinearLayout ll3 = new LinearLayout(this);
    24.                         ll3.setOrientation(LinearLayout.HORIZONTAL);
    25.                         ll3.setLayoutParams(params);
    26.                         ll.addView(ll3);
    27.                        
    28.                         LinearLayout ll4 = new LinearLayout(this);
    29.                         ll4.setOrientation(LinearLayout.VERTICAL);
    30.                         ll4.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
    31.                        
    32.                         final int id = cursor1.getInt(0);
    33.                        
    34.                         ImageView iv = new ImageView(this);
    35.                         iv.setImageURI(uri2);
    36.                         iv.setAdjustViewBounds(true);
    37.                         iv.setMaxHeight(100);
    38.                         iv.setMaxWidth(100);
    39.                         iv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    40.                         iv.setClickable(true);
    41.                         iv.setOnClickListener(new OnClickListener(){
    42.  
    43.                             @Override
    44.                             public void onClick(View v) {
    45.                                 Intent i = new Intent(PicVid.this, Picture.class);
    46.                                 //create bundle and initialize
    47.                                 Bundle bundle = new Bundle();
    48.                                 //add parameters to bundle
    49.                                 bundle.putString("uri", uri2.toString());
    50.                                 //add bundle to the intent
    51.                                 i.putExtras(bundle);
    52.                                 startActivity(i);
    53.                             }});
    54.                         //((LinearLayout.LayoutParams) iv.getLayoutParams()).gravity = Gravity.CENTER;
    55.                         ll3.addView(iv);
    56.                         ll3.addView(ll4);
    57.                        
    58.                         TextView captionTitle = new TextView(this);
    59.                         captionTitle.setText("Caption:");
    60.                         ll4.addView(captionTitle);
    61.                        
    62.                         TextView caption = new TextView(this);
    63.                         caption.setText(cursor1.getString(1));
    64.                         ll4.addView(caption);
    65.                        
    66.                         final View ruler3 = new  View(this);
    67.                         ruler3.setBackgroundColor(0xFF909090);
    68.                         ruler3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 2));
    69.                                          
    70.                         ImageButton delImage = new ImageButton(this);
    71.                         delImage.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    72.                         delImage.setImageResource(R.drawable.delete);
    73.                         delImage.setOnClickListener(new OnClickListener(){
    74.  
    75.                             @Override
    76.                             public void onClick(View v) {
    77.                                 db.open();
    78.                                 db.deleteItem("picvid", id, iid);
    79.                                 db.close();
    80.                                 ll.removeView(ll3);
    81.                                 ll.removeView(ruler3);
    82.                             }
    83.                            
    84.                         });
    85.                         ((LinearLayout.LayoutParams) delImage.getLayoutParams()).gravity = Gravity.CENTER;
    86.                         ll3.addView(delImage);
    87.                        
    88.                         ll.addView(ruler3);
    89.                     }
    90.                 }
    91.                 cursor1.moveToNext();
    92.             }
    93.         }
    94.         cursor1.close();
    95.         db.close();
    96.  
    Code (Text):
    1. 06-10 09:23:34.612: ERROR/AndroidRuntime(800): Uncaught handler: thread main exiting due to uncaught exception
    2. 06-10 09:23:34.622: ERROR/AndroidRuntime(800): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
    3. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    4. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:375)
    5. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:212)
    6. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:663)
    7. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.graphics.drawable.Drawable.createFromStream(Drawable.java:641)
    8. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.widget.ImageView.resolveUri(ImageView.java:491)
    9. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.widget.ImageView.setImageURI(ImageView.java:287)
    10. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at  mobile.PicVid.onCreate(PicVid.java:202)
    11. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at  mobile.PicVid.onResume(PicVid.java:701)
    12. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1228)
    13. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.Activity.performResume(Activity.java:3542)
    14. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2619)
    15. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:170)
    16. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:518)
    17. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.ActivityGroup.onResume(ActivityGroup.java:58)
    18. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1228)
    19. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.Activity.performResume(Activity.java:3542)
    20. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2619)
    21. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2647)
    22. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2287)
    23. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3278)
    24. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.ActivityThread.access$1900(ActivityThread.java:112)
    25. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
    26. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.os.Handler.dispatchMessage(Handler.java:99)
    27. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.os.Looper.loop(Looper.java:123)
    28. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at android.app.ActivityThread.main(ActivityThread.java:3948)
    29. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at java.lang.reflect.Method.invokeNative(Native Method)
    30. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at java.lang.reflect.Method.invoke(Method.java:521)
    31. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
    32. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
    33. 06-10 09:23:34.622: ERROR/AndroidRuntime(800):     at dalvik.system.NativeStart.main(Native Method)
     

    Advertisement
  2. j_p36

    j_p36 Member This Topic's Starter

    Joined:
    May 19, 2010
    Messages:
    16
    Likes Received:
    0
    Ok, so the problem is not having final in the code.

    I edited the code to remove almost all the finals and the log still gives the same error.

    The pictures still are not being released and I'm not sure why.

    here is my new code

    Code (Text):
    1.  
    2.         db.open();
    3.         Cursor cursor1 = db.getItemFromInfo("picvid", iid);
    4.         if(cursor1.moveToFirst())
    5.         {
    6.             while(!cursor1.isAfterLast())
    7.             {
    8.                 String name="";
    9.                 String uriString = cursor1.getString(2);
    10.                 final Uri uri2 = Uri.parse(uriString);
    11.                
    12.                 String[] projection = { MediaStore.Images.ImageColumns.DATA, /*col1*/
    13.                                 MediaStore.Images.ImageColumns.DISPLAY_NAME /*col2*/};
    14.  
    15.                 Cursor c = managedQuery(uri2, projection, null, null, null);
    16.                 if (c!=null && c.moveToFirst()) {
    17.                         name = c.getString(1);
    18.                         Log.d("Display name",name);
    19.                 }
    20.                 c.close();
    21.                
    22.                 if(!name.equals(""))
    23.                 {
    24.                     if(name.toLowerCase().contains(".jpg"))
    25.                     {
    26.                         LinearLayout ll5 = new LinearLayout(this);
    27.                         ll5.setOrientation(LinearLayout.VERTICAL);
    28.                         ll5.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    29.                         ll.addView(ll5);
    30.                        
    31.                         LinearLayout ll3 = new LinearLayout(this);
    32.                         ll3.setOrientation(LinearLayout.HORIZONTAL);
    33.                         ll3.setLayoutParams(params);
    34.                         ll5.addView(ll3);
    35.                        
    36.                         LinearLayout ll4 = new LinearLayout(this);
    37.                         ll4.setOrientation(LinearLayout.VERTICAL);
    38.                         ll4.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1));
    39.                        
    40.                         final int id = cursor1.getInt(0);
    41.                        
    42.                         ImageView iv = new ImageView(this);
    43.                         iv.setImageURI(uri2);
    44.                         iv.setAdjustViewBounds(true);
    45.                         iv.setMaxHeight(100);
    46.                         iv.setMaxWidth(100);
    47.                         iv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    48.                         iv.setClickable(true);
    49.                         iv.setOnClickListener(new OnClickListener(){
    50.  
    51.                             @Override
    52.                             public void onClick(View v) {
    53.                                 Intent i = new Intent(PicVid.this, Picture.class);
    54.                                 //create bundle and initialize
    55.                                 Bundle bundle = new Bundle();
    56.                                 //add parameters to bundle
    57.                                 bundle.putString("uri", uri2.toString());
    58.                                 //add bundle to the intent
    59.                                 i.putExtras(bundle);
    60.                                 startActivity(i);
    61.                             }});
    62.                         //((LinearLayout.LayoutParams) iv.getLayoutParams()).gravity = Gravity.CENTER;
    63.                         ll3.addView(iv);
    64.                         ll3.addView(ll4);
    65.                        
    66.                         TextView captionTitle = new TextView(this);
    67.                         captionTitle.setText("Caption:");
    68.                         ll4.addView(captionTitle);
    69.                        
    70.                         TextView caption = new TextView(this);
    71.                         caption.setText(cursor1.getString(1));
    72.                         ll4.addView(caption);
    73.                        
    74.                         View ruler3 = new  View(this);
    75.                         ruler3.setBackgroundColor(0xFF909090);
    76.                         ruler3.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 2));
    77.                                          
    78.                         ImageButton delImage = new ImageButton(this);
    79.                         delImage.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    80.                         delImage.setImageResource(R.drawable.delete);
    81.                         delImage.setOnClickListener(new OnClickListener(){
    82.  
    83.                             @Override
    84.                             public void onClick(View v) {
    85.                                 LinearLayout ll3 = (LinearLayout) v.getParent();
    86.                                 LinearLayout ll5 = (LinearLayout) ll3.getParent();
    87.                                 db.open();
    88.                                 db.deleteItem("picvid", id, iid);
    89.                                 db.close();
    90.                                 ll.removeView(ll5);
    91. //                                ll.removeView(ll3);
    92. //                                ll.removeView(ruler3);
    93.                             }
    94.                            
    95.                         });
    96.                         ((LinearLayout.LayoutParams) delImage.getLayoutParams()).gravity = Gravity.CENTER;
    97.                         ll3.addView(delImage);
    98.                        
    99.                         ll5.addView(ruler3);
    100.                     }
    101.             }
    102.                 cursor1.moveToNext();
    103.             }
    104.         }
    105.         cursor1.close();
    106.         db.close();
    107.  
     
  3. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    Can you destruct the image in onPause and reload it in onResume?
     
  4. king angels

    king angels New Member

    Joined:
    Jun 11, 2010
    Messages:
    3
    Likes Received:
    0
    Well first things first make sure you have the most recent drivers for your video card and all the updates for vista. SP1 should have fixed any memory leak issues. from what i'm seeing on the specs of it yeah you shouldn't have any problems with WoW now crysis and fear you probably are pushing it with only 2GB of RAM. I would leave the virtual RAM to be set by the system just tends to be better unless your dealing with a server.
     
  5. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    As you've already worked out, declaring something as final or not is irrelevant to your problem, and has no effect on the amount of memory being used.

    The comment about Vista and video drivers is also irrelevant, as it's Android that is running out of memory. (The fact that the emulator may be running on Vista makes no difference.)

    The problem is that images and bitmaps use up a lot of memory, and in my experience I've managed to have 2 at the most before running out of memory (they were fairly big ones). You need to release the resource to free up the memory before you try to allocate any more.

    Mark
     
  6. j_p36

    j_p36 Member This Topic's Starter

    Joined:
    May 19, 2010
    Messages:
    16
    Likes Received:
    0
    How would I go about destructing the image when its set like this?

    Code (Text):
    1.  
    2. ImageView iv = new ImageView(this);
    3. iv.setImageURI(uri2);
    Also, I found a temporary work around but its not a permanent fix.

    Code (Text):
    1.  
    2. private BitmapFactory.Options options;
    3. ...
    4. options=new BitmapFactory.Options();
    5. options.inSampleSize = 8;
    6. ...
    7. Bitmap preview = BitmapFactory.decodeFile(uriString, options);
    8. ImageView iv = new ImageView(this);
    9. //iv.setImageURI(uri2);
    10. iv.setImageBitmap(preview);
    11.  
    It seems that since I shrink the image before doing anything with it I can load a lot of images and rotate as much as I want without reaching the memory cap. But I'm still not sure if these images are being released or not.

    Thanks for any help.
     
  7. cp1

    cp1 Well-Known Member

    Joined:
    Apr 30, 2010
    Messages:
    375
    Likes Received:
    35
    Oof I thought it would be something simple like:

    delete iv;

    But I was looking around and it seems that Java is supposed to do this automatically for you -- it seems that the answer is to resize as you found, and then android will deallocate the memory when it's not needed. You might try to removeView(yourimageview) perhaps if you manually want to delete it.
     
    j_p36 likes this.

Share This Page

Loading...