Hi everyone,

I would like to share some findings related to using images on
activities and impact on an application creating multiples activities
or changing orientation.

What I coded:

public class BigImageActivity extends Activity {
    int count;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        if (intent != null) {
            count = intent.getIntExtra("count", 0);
        }

        setContentView(R.layout.bigimagelayout);

        TextView txtView = (TextView) findViewById(R.id.Counter);
        txtView.setText("Activity: " + count);

        Button openNew = (Button) findViewById(R.id.OpenNew);
        openNew.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startNewActivity(count);
            }
        });
    }

    public void startNewActivity(int count) {
        Intent intent = new Intent();
        intent.setClass(this, BigImageActivity.class);
        intent.putExtra("count", count + 1);
        startActivity(intent);

    }
}

Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android";
  android:layout_width="match_parent"
  android:layout_height="wrap_content" android:orientation="vertical">
    <Button
        android:layout_height="wrap_content"
        android:id="@+id/OpenNew"
        android:layout_width="match_parent"
        android:text="Open New" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="0"
        android:id="@+id/Counter" />
    <ImageView
        android:id="@+id/BigImage"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:scaleType="fitCenter"
        android:src="@drawable/domo2048" />
</LinearLayout>

Image resource called domo2048 its an JPEG mage of 2048x1536 and about
627Kb (physical size)

I was able to create... I think stack is better ;), about 233
activities using that button. I got my app killed during the process
but Android was able to recreate them,so I was able to go back through
all activities. Curiously I got my app killed several times too while
presing back till activity 0.

The second finding was that, with only one activity, but changing
orientation I got an application crash afte a ramdom number of
orientation changes ( some times at third try, some times higher
number, depending of how fast I did it between changes)

What I got on my logcat was the following:

04-27 17:36:48.217: INFO/WindowManager(1574): Setting rotation to 1,
animFlags=0
04-27 17:36:48.237: INFO/ActivityManager(1574): Config changed:
{ scale=1.0 imsi=712/2 loc=en_US touch=3 keys=2/1/2 nav=2/1 orien=2
layout=34 uiMode=17 seq=402}
04-27 17:36:48.277: ERROR/dalvikvm-heap(25598): Heap Massage needed
(14155776-byte external allocation too big)
04-27 17:36:48.277: ERROR/dalvikvm-heap(25598): Full GC (don't collect
SoftReferences)
04-27 17:36:48.297: INFO/UsageStats(1574): Unexpected resume of
com.schematic.android while already resumed in com.schematic.android
04-27 17:36:48.307: DEBUG/dalvikvm(25598): GC_EXTERNAL_ALLOC freed 352
objects / 22840 bytes in 32ms
04-27 17:36:48.317: INFO/GPSButton(1574): Update State
04-27 17:36:48.397: ERROR/dalvikvm-heap(25598): Heap Massage was
successful...

I got it several times, before get this:

04-27 17:36:51.057: ERROR/AndroidRuntime(25598):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.schematic.android.grindr/
com.schematic.android.BigImageActivity}:
android.view.InflateException: Binary XML file line #8: Error
inflating class <unknown>
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2663)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
2679)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:
3815)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.app.ActivityThread.access$2400(ActivityThread.java:125)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.os.Handler.dispatchMessage(Handler.java:99)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.os.Looper.loop(Looper.java:123)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.app.ActivityThread.main(ActivityThread.java:4627)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
java.lang.reflect.Method.invokeNative(Native Method)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
java.lang.reflect.Method.invoke(Method.java:521)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
dalvik.system.NativeStart.main(Native Method)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598): Caused by:
android.view.InflateException: Binary XML file line #8: Error
inflating class <unknown>
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.view.LayoutInflater.createView(LayoutInflater.java:513)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:
56)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.view.LayoutInflater.inflate(LayoutInflater.java:407)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:
198)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.app.Activity.setContentView(Activity.java:1647)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
com.schematic.android.BigImageActivity.onCreate(BigImageActivity.java:
23)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
1047)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
2627)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     ... 12 more
04-27 17:36:51.057: ERROR/AndroidRuntime(25598): Caused by:
java.lang.reflect.InvocationTargetException
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.widget.ImageView.<init>(ImageView.java:108)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
java.lang.reflect.Constructor.constructNative(Native Method)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
java.lang.reflect.Constructor.newInstance(Constructor.java:446)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.view.LayoutInflater.createView(LayoutInflater.java:500)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     ... 23 more
04-27 17:36:51.057: ERROR/AndroidRuntime(25598): Caused by:
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.graphics.Bitmap.nativeCreate(Native Method)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.graphics.Bitmap.createBitmap(Bitmap.java:468)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.graphics.Bitmap.createBitmap(Bitmap.java:435)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.graphics.Bitmap.createScaledBitmap(Bitmap.java:340)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:
323)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:
697)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.content.res.Resources.loadDrawable(Resources.java:1767)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.content.res.TypedArray.getDrawable(TypedArray.java:601)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     at
android.widget.ImageView.<init>(ImageView.java:118)
04-27 17:36:51.057: ERROR/AndroidRuntime(25598):     ... 27 more
04-27 17:36:51.067: WARN/ActivityManager(1574):   Force finishing
activity com.schematic.android.grindr/.BigImageActivity

I did use the heap monitor tool on Eclipse, and application heap was
never above 4Mb. As far as I understand, befor Android 3.0 bitmap heap
size is not "available".

My concern is with UIs that uses a lot of images and may be affected
when user changes orientation several times.

Is there a way to release memory of images that are used on layouts?

Should I get the drawables during runtime (using getDrawable) and set
it on my views when needed similarly as
http://android.git.kernel.org/?p=platform/packages/apps/Launcher.git;a=blob;f=src/com/android/launcher/LauncherModel.java;h=0ef2a806b767142b28b2ff3b37f21f4ca16c355d;hb=cupcake

If some Android Developer/Engineer wants to jump in and correct me I
would be more than happy :D

-f4





-- 
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