I made a test about memory comsumption loading bitmaps. It runs fine on a 
device - it doesn't occupy even 2 mb, but on the emulator it soon occupies 
20 mb.

Both the device and the emulator have resolution 480 x 800 and both are 
running Android 2.3.3. In both cases, I also outputted the dimenstions of 
the loaded bitmaps, and they are the same - 640 x 588.

This is the test class:

public class TestActivity extends Activity {

ImageView imageView;
 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); 
 showDeviceResolution();
 Log.d("memory", "stats before view");
showMemoryStats();
 setContentView(R.layout.mylayout);
 Log.d("memory", "stats after view");
showMemoryStats();
 imageView = (ImageView)findViewById(R.id.image);
 }
 @Override
public void onWindowFocusChanged(boolean hasFocus) {
Log.d("memory", "stats before animation");
showMemoryStats();
 initTest();
 super.onWindowFocusChanged(hasFocus);
}
 private void initTest() { 
Drawable b1 = getResources().getDrawable(R.drawable.b1);
Log.d("test", "bitmap1 dim: " + ((BitmapDrawable)b1).getBitmap().getWidth() 
+ " x " + ((BitmapDrawable)b1).getBitmap().getHeight());
imageView.setImageDrawable(b1);
showMemoryStats();
Drawable b2 = getResources().getDrawable(R.drawable.b2);
Log.d("test", "bitmap2 dim: " + ((BitmapDrawable)b2).getBitmap().getWidth() 
+ " x " + ((BitmapDrawable)b2).getBitmap().getHeight());
imageView.setImageDrawable(b2);
showMemoryStats();
Drawable b3 = getResources().getDrawable(R.drawable.b3);
imageView.setImageDrawable(b3);
showMemoryStats();
Drawable b4 = getResources().getDrawable(R.drawable.b4);
imageView.setImageDrawable(b4);
showMemoryStats();
Drawable b5 = getResources().getDrawable(R.drawable.b5);
imageView.setImageDrawable(b5);
showMemoryStats();
Drawable b6 = getResources().getDrawable(R.drawable.b6);
imageView.setImageDrawable(b6);
showMemoryStats();
Drawable b7 = getResources().getDrawable(R.drawable.b7);
imageView.setImageDrawable(b7);
showMemoryStats();
Drawable b8 = getResources().getDrawable(R.drawable.b8);
imageView.setImageDrawable(b8);
showMemoryStats();
Drawable b9 = getResources().getDrawable(R.drawable.b9);
imageView.setImageDrawable(b9);
showMemoryStats();
Drawable b10 = getResources().getDrawable(R.drawable.b10);
imageView.setImageDrawable(b10);
showMemoryStats();
Drawable b11 = getResources().getDrawable(R.drawable.b11);
imageView.setImageDrawable(b11);
showMemoryStats();
Drawable b12 = getResources().getDrawable(R.drawable.b12);
imageView.setImageDrawable(b12);
showMemoryStats();
Drawable b13 = getResources().getDrawable(R.drawable.b13);
imageView.setImageDrawable(b13);
showMemoryStats();
Drawable b14 = getResources().getDrawable(R.drawable.b14);
imageView.setImageDrawable(b14);
showMemoryStats();
Drawable b15 = getResources().getDrawable(R.drawable.b15);
imageView.setImageDrawable(b15);
showMemoryStats();
Drawable b16 = getResources().getDrawable(R.drawable.b16);
imageView.setImageDrawable(b16);
showMemoryStats();
Drawable b17 = getResources().getDrawable(R.drawable.b17);
imageView.setImageDrawable(b17);
showMemoryStats();
}
 public static void showMemoryStats() {
double nativeUsage = Debug.getNativeHeapAllocatedSize(); 
Log.i("memory", "nativeUsage: " + (nativeUsage / 1048576d));
}
 public void showDeviceResolution() {
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
String str = "resolution: " + dm.widthPixels + " x " + dm.heightPixels;
Log.d("memory", "resolution: " + str);
}
}

On the device the native usage increments only a bit each time, and as said 
at the end is still under 2mb, on the other side the emulator loads more 
than 1mb each time and at the end runs out of memory, because it loaded 
more than 20mb...

Can somebody explain me what's going on here...

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

Reply via email to