Memory leak in the application (loading, saving and cycling between them)
I am creating an application that downloads 2 images from a server, saves them to the SD card and displays them (loops, changing the image every 15 seconds).
I finished, but after a couple of minutes the program crashes: OutOfMemoryError: bitmap exceeds VM budget.
So, there must be a memory leak somewhere. I'm still pretty new to Android programming: not too sure what kind of cleanup I have to do to keep the VM out.
Any help?
private ImageView mImageView;
private Bitmap mImageBitmap;
public static String rslt="";
/** Called when the activity is first created. */
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
mImageView = (ImageView) findViewById(R.id.imageView1);
mImageBitmap=null;
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int count = 1;
public void run() {
Bitmap bm = getNewImages();
if (bm != null){
mImageView.setImageBitmap(bm);
bm=null;
System.gc();
handler.postDelayed(this, 15* 1000);
}else if(count==1){
File img1 = new File(Environment.getExternalStorageDirectory(), "1.jpg");
if(img1.exists()){
bm = BitmapFactory.decodeFile(img1.getAbsolutePath());
if (bm != null){
mImageView.setImageBitmap(bm);
bm=null;
}
}
count = 2;
System.gc();
handler.postDelayed(this, 15* 1000);
}else if (count==2){
File img2 = new File(Environment.getExternalStorageDirectory(), "2.jpg");
if(img2.exists()){
bm = BitmapFactory.decodeFile(img2.getAbsolutePath());
if (bm != null){
mImageView.setImageBitmap(bm);
bm=null;
}
}
count=1;
System.gc();
handler.postDelayed(this, 15* 1000);
}
}
private Bitmap getNewImages() {
ConnectivityManager connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
mImageBitmap=null;
if (mWifi.isConnected()) {
try{
// EditText ed1=(EditText)findViewById(R.id.editText1);
// String rego=ed1.getText().toString();
String rego="AGR905";
rslt="START";
Caller c=new Caller();
c.rego=rego;
c.join();
c.start();
while(rslt=="START")
{
try
{
Thread.sleep(10);
}
catch(Exception ex)
{
}
}
}
catch(Exception ex){
}
if (rslt.length()<=1600){
}else{
byte[] decodedString = Base64.decode(rslt, Base64.DEFAULT);
mImageBitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
mImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File from = new File(Environment.getExternalStorageDirectory(), "1.jpg");
if(from.exists()){
File to = new File(Environment.getExternalStorageDirectory(), "2.jpg");
from.renameTo(to);
from.delete();
}
//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "1.jpg");
try {
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return mImageBitmap;
}
};
handler.postDelayed(runnable, 2000); //for initial delay..
source to share
There are a few things that should help smooth out the memory here.
- Run bitmap.recycle () after you're done with the bitmap.
- Then set them to null.
- Run the garbage collector every pass.
System.gc()
... - Make sure you are requesting the big heap from the manifest.
android:largeHeap="true"
into the application. - Working with memory profiling in the application. See this answer for more details on how to do this.
source to share
I don't think the issue is memory leak. You have probably exceeded your memory budget while keeping both images in memory at the same time. Remember, each bitmap uses height_in_px * width_in_px * 4 bytes , and your application's memory budget is 24 ... 48 MB (device dependent).
There's a great tutorial on using bitmaps effectively. Use it to find out what the problem is and fix it.
source to share