[android-developers] Re: Memory leaks when using bitmaps in android

2011-12-22 Thread String
When chasing a problem like this a couple of years ago, my testing seemed to 
indicate that setting a bitmap to null right after a call to recycle() was 
leaky. It was as though the recycle call needed some time to work, and setting 
null got in the way. I found it was better to just call recycle on its own, and 
then let the GC deal with the empty bitmap object in its own time.

String

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


Re: [android-developers] Re: Memory leaks when using bitmaps in android

2011-12-22 Thread Miguel Morales
Working with bitmaps is a little hard on Android.
One thing to do is to resize or pack them so that they take up less memory.
Another thing you may want to look into is to reuse the bitmap
object/memory.  You may have to do this in native though.
As other have pointed out System.gc() just gives the system a hint that it
needs to run the garbage collector.

Good luck.

On Thu, Dec 22, 2011 at 12:11 AM, String sterling.ud...@googlemail.comwrote:

 When chasing a problem like this a couple of years ago, my testing seemed
 to indicate that setting a bitmap to null right after a call to recycle()
 was leaky. It was as though the recycle call needed some time to work, and
 setting null got in the way. I found it was better to just call recycle on
 its own, and then let the GC deal with the empty bitmap object in its own
 time.

 String

 --
 You received this message because you are subscribed to the Google
 Groups Android Developers group.
 To post to this group, send email to android-developers@googlegroups.com
 To unsubscribe from this group, send email to
 android-developers+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/android-developers?hl=en




-- 
~ Jeremiah:9:23-24
Android 2D MMORPG: http://solrpg.com/,
http://www.youtube.com/user/revoltingx

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

[android-developers] Re: Memory leaks when using bitmaps in android

2011-12-21 Thread shyama
Sorry for the large code. Thanks for your help. What I would like to
know is when to use weak references and if it is a good option while
using Bitmaps? If so how does it help?

/** This is the onPause function which releases the bitmap when the
imagebutton is clicked and gallery is displayed.
 protected void onPause() {
super.onPause();
Log.v(TAG,on pause called);
Log.v(TAG,Allocated memoryin on pause before recycle done:
+Debug.getNativeHeapAllocatedSize());
choice.setImageBitmap(null);
if(bitmap != null)
{
Log.v(TAG,bitmap is not null);
bitmap.recycle();
if(bitmap.isRecycled())
Log.v(TAG,bitmap is recycled);
bitmap = null;
}


if(mutBitmap != null)
{
mutBitmap.recycle();
if(mutBitmap.isRecycled())
Log.v(TAG,mutBitmap is recycled);
mutBitmap = null;
}
Log.v(TAG,Allocated memory on pause after recycle done:  
+Debug.getNativeHeapAllocatedSize());
}
@Override
protected void onResume() {
super.onResume();
choice.setImageBitmap(bitmap);
}
  /** call to Gallery Activity **/
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Log.v(TAG,onActivityResult called before loading bitmap,
Allocated memory:  +Debug.getNativeHeapAllocatedSize());


if (resultCode == RESULT_OK) {
photoUri  = intent.getData();


if (photoUri != null) {
try {
Log.v(TAG, No crash 0);
   bitmap =
Bitmap.createScaledBitmap(MediaStore.Images.Media.getBitmap(this.getContentResolver(),
photoUri), 300, 400, true);
   Log.v(TAG, No crash 1);
   Log.v(TAG,onActivityResult called after
loading bitmap,  Allocated memory:
+Debug.getNativeHeapAllocatedSize());
  choice.setImageBitmap(bitmap);

} catch (Exception e) {
Log.v(TAG, Unable to load image at path:  +
photoUri.getPath());
}
}
}


}

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en


[android-developers] Re: Memory leaks when using bitmaps in android

2011-12-20 Thread shyama
I realised that when when I made a call to System.gc() right after the
bitmao..recycle() call the heap space  is getting returned to the VM
heap. But I have been reading about Garbage collection and it seems
that calling System.gc() is not a good programming practice. Is there
another way to make sure that the garbage collection happens right
after the bitmap is recycled? Would it be helpful to implement Weak
references for this scenario?


 to On Dec 19, 3:10 pm, shyama shyama.asoka...@gmail.com wrote:
 I know this question has been asked over and over again in these
 forums. But the reason I am posting it again is because I have nt
 received a clear answer to it. It would be great if someone could help
 me with this.

 I am current using opencv + android to make some simple app that will
 do image manipulation like edge, blur etc and display it using an
 imagebutton. The images are being loaded through the gallery. There
 are 3 buttons to blur, edge, and reset to original which will display
 the modified image on the image button. given below is my code.

     package com.example;

     import java.io.File;
     import java.io.FileNotFoundException;
     import java.io.IOException;
     import java.io.RandomAccessFile;
     import java.nio.Buffer;
     import java.nio.MappedByteBuffer;
     import java.nio.channels.FileChannel;
     import java.nio.channels.FileChannel.MapMode;

     import android.app.Activity;
     import android.content.Intent;
     import android.graphics.*;
     import android.graphics.Bitmap.Config;
     import android.net.Uri;
     import android.os.Bundle;
     import android.os.Debug;
     import android.provider.MediaStore;
     import android.util.Log;
     import android.view.View;
     import android.view.View.OnClickListener;
     import android.widget.Button;
     import android.widget.ImageButton;
     import android.widget.ImageView;

     import org.opencv.core.*;
     import org.opencv.imgproc.*;
     import org.opencv.android.*;
     import org.opencv.highgui.*;

     import dalvik.system.VMRuntime;

     import android.graphics.Color;

     public class ImageProcessing extends Activity implements
 View.OnClickListener{

     private ImageButton choice;
     private Button Blur;
     private Button Canny;
     private Button Reset;
     Uri photoUri;
     static private Bitmap bitmap;
     static private Bitmap mutBitmap;

     private static String TAG = ImageProcessing;
     private static int TAKE_GALLERY_REQUEST = 2;
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.img);

     choice = (ImageButton) findViewById(R.id.imagechoice);

     Blur = (Button) findViewById(R.id.Blur);
     Canny = (Button) findViewById(R.id.edge);
     Reset = (Button) findViewById(R.id.reset);

     choice.setOnClickListener(this);
     Blur.setOnClickListener(this);
     Canny.setOnClickListener(this);
     Reset.setOnClickListener(this);

     }
     @Override
     public void onClick(View v) {

     if(v.getId() == R.id.imagechoice)
     {
      Intent pickPhoto = new Intent(Intent.ACTION_PICK);
      pickPhoto.setType(image/*);
      startActivityForResult(pickPhoto, TAKE_GALLERY_REQUEST);
     }
     if(v.getId() == R.id.Blur)
     {

       if(mutBitmap != null)
         mutBitmap.recycle();
       mutBitmap = bitmap.copy(Config.ARGB_, true);
       choice.setImageBitmap(mutBitmap);
       Mat mImg = Utils.bitmapToMat(mutBitmap);
       Size ksize = new Size(15,15);
       Imgproc.blur(mImg, mImg, ksize);
       mutBitmap.recycle();
       mutBitmap = Bitmap.createBitmap(mImg.cols(), mImg.rows(),
 Bitmap.Config.ARGB_);
       Utils.matToBitmap(mImg, mutBitmap);
       choice.setImageBitmap(mutBitmap);
     }
     if(v.getId() == R.id.edge)
     {
       if(mutBitmap != null)
         mutBitmap.recycle();
       mutBitmap = bitmap.copy(Config.ARGB_, true);
       choice.setImageBitmap(mutBitmap);
       Mat mImg = new Mat();
       mImg = Utils.bitmapToMat(mutBitmap);

       /**Converting to grayscale**/
       Mat mGray = new Mat(mImg.rows(), mImg.cols(), CvType.CV_8UC1,
 new Scalar(0));
       Imgproc.cvtColor(mImg , mGray, Imgproc.COLOR_BGRA2GRAY, 4);
       /**Applying Canny**/
       Imgproc.Canny(mGray, mGray, 80, 90);

       //Converting back to 4 channel image
       Imgproc.cvtColor(mGray , mImg, Imgproc.COLOR_GRAY2RGBA, 4);
       mutBitmap.recycle();
       mutBitmap = Bitmap.createBitmap(mImg.cols(), mImg.rows(),
 Bitmap.Config.ARGB_);
       Utils.matToBitmap(mImg, mutBitmap);
       choice.setImageBitmap(mutBitmap);
     }
     if(v.getId() == R.id.reset)
     {
       choice.setImageBitmap(bitmap);
     }
     }
     @Override
     protected void onPause() {
     super.onPause();
     Log.v(TAG,on pause called);

     Log.v(TAG,Allocated memoryin on pause before recycle done:
     

Re: [android-developers] Re: Memory leaks when using bitmaps in android

2011-12-20 Thread James Black
You should work on getting a smaller example to demonstrate as this is too
much code to read through.

It may also help you solve the problem yourself.
On Dec 20, 2011 3:21 PM, shyama shyama.asoka...@gmail.com wrote:

 I realised that when when I made a call to System.gc() right after the
 bitmao..recycle() call the heap space  is getting returned to the VM
 heap. But I have been reading about Garbage collection and it seems
 that calling System.gc() is not a good programming practice. Is there
 another way to make sure that the garbage collection happens right
 after the bitmap is recycled? Would it be helpful to implement Weak
 references for this scenario?


  to On Dec 19, 3:10 pm, shyama shyama.asoka...@gmail.com wrote:
  I know this question has been asked over and over again in these
  forums. But the reason I am posting it again is because I have nt
  received a clear answer to it. It would be great if someone could help
  me with this.
 
  I am current using opencv + android to make some simple app that will
  do image manipulation like edge, blur etc and display it using an
  imagebutton. The images are being loaded through the gallery. There
  are 3 buttons to blur, edge, and reset to original which will display
  the modified image on the image button. given below is my code.
 
  package com.example;
 
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.io.RandomAccessFile;
  import java.nio.Buffer;
  import java.nio.MappedByteBuffer;
  import java.nio.channels.FileChannel;
  import java.nio.channels.FileChannel.MapMode;
 
  import android.app.Activity;
  import android.content.Intent;
  import android.graphics.*;
  import android.graphics.Bitmap.Config;
  import android.net.Uri;
  import android.os.Bundle;
  import android.os.Debug;
  import android.provider.MediaStore;
  import android.util.Log;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ImageButton;
  import android.widget.ImageView;
 
  import org.opencv.core.*;
  import org.opencv.imgproc.*;
  import org.opencv.android.*;
  import org.opencv.highgui.*;
 
  import dalvik.system.VMRuntime;
 
  import android.graphics.Color;
 
  public class ImageProcessing extends Activity implements
  View.OnClickListener{
 
  private ImageButton choice;
  private Button Blur;
  private Button Canny;
  private Button Reset;
  Uri photoUri;
  static private Bitmap bitmap;
  static private Bitmap mutBitmap;
 
  private static String TAG = ImageProcessing;
  private static int TAKE_GALLERY_REQUEST = 2;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.img);
 
  choice = (ImageButton) findViewById(R.id.imagechoice);
 
  Blur = (Button) findViewById(R.id.Blur);
  Canny = (Button) findViewById(R.id.edge);
  Reset = (Button) findViewById(R.id.reset);
 
  choice.setOnClickListener(this);
  Blur.setOnClickListener(this);
  Canny.setOnClickListener(this);
  Reset.setOnClickListener(this);
 
  }
  @Override
  public void onClick(View v) {
 
  if(v.getId() == R.id.imagechoice)
  {
   Intent pickPhoto = new Intent(Intent.ACTION_PICK);
   pickPhoto.setType(image/*);
   startActivityForResult(pickPhoto, TAKE_GALLERY_REQUEST);
  }
  if(v.getId() == R.id.Blur)
  {
 
if(mutBitmap != null)
  mutBitmap.recycle();
mutBitmap = bitmap.copy(Config.ARGB_, true);
choice.setImageBitmap(mutBitmap);
Mat mImg = Utils.bitmapToMat(mutBitmap);
Size ksize = new Size(15,15);
Imgproc.blur(mImg, mImg, ksize);
mutBitmap.recycle();
mutBitmap = Bitmap.createBitmap(mImg.cols(), mImg.rows(),
  Bitmap.Config.ARGB_);
Utils.matToBitmap(mImg, mutBitmap);
choice.setImageBitmap(mutBitmap);
  }
  if(v.getId() == R.id.edge)
  {
if(mutBitmap != null)
  mutBitmap.recycle();
mutBitmap = bitmap.copy(Config.ARGB_, true);
choice.setImageBitmap(mutBitmap);
Mat mImg = new Mat();
mImg = Utils.bitmapToMat(mutBitmap);
 
/**Converting to grayscale**/
Mat mGray = new Mat(mImg.rows(), mImg.cols(), CvType.CV_8UC1,
  new Scalar(0));
Imgproc.cvtColor(mImg , mGray, Imgproc.COLOR_BGRA2GRAY, 4);
/**Applying Canny**/
Imgproc.Canny(mGray, mGray, 80, 90);
 
//Converting back to 4 channel image
Imgproc.cvtColor(mGray , mImg, Imgproc.COLOR_GRAY2RGBA, 4);
mutBitmap.recycle();
mutBitmap = Bitmap.createBitmap(mImg.cols(), mImg.rows(),
  Bitmap.Config.ARGB_);
Utils.matToBitmap(mImg, 

[android-developers] Re: Memory leaks when using bitmaps in android

2011-12-20 Thread lbendlin
A call to System.gc() doesn't actually start the GC process. All it does is 
add the request to the queue. The operating system will then decide at its 
leisure when to actually do the GC. So even calling System.gc() repeatedly 
(as is sometimes recommended) is just another word for desperation.

-- 
You received this message because you are subscribed to the Google
Groups Android Developers group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en