0

I'm prototyping a simple app and have one activity with one fragment. The fragment is updated when the user clicks next to rotate through three screens.

I've defined 3 horizontal and vertical relative layouts using dp for the imageview, where each pair of screens has an image included at 50dp*50dp, or 150dp * 150 dp.

The three image ranges from 200k to 400k on disk (no resolution specific versions yet). When I look at my apps memory consumption in ADM I see that my app has somehow run off with 18MB:

1-byte array [byte,bool] Count: 506, Total size:18MB

I thought this might be related to android scaling bitmaps ineffectively, so I moved my assets into drawable-nodpi. This sped up rendering on the emulator (strange given that they were small images to begin with), but didn't impact memory allocation.

To debug, I created a different starting activity, not loading the activity with images until a button is pressed. I was hoping I could use the allocation tracker to hunt down issue. Once again, at start time, the app still uses 18MB of memory, so I couldn't figure out where it's being allocated.

Any ideas what might be going on?

Adding some code as requested as well:

1) One of the layout files:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:gravity="center">

    <TextView
        android:id="@+id/textView"
        android:text="@string/FRE_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:editable="false"
        android:autoText="false"
        android:clickable="false"
        android:focusable="false"
        android:enabled="true"
        android:padding="10dp"
        />


    <ImageView
        android:id="@+id/freImage1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
       android:src="@drawable/fre_1" />

<ImageButton
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/imageButton"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/right_arrow" />

    <Button
        android:id="@+id/buttonGetStarted"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Get_started"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:onClick="LoadLogin" />

</RelativeLayout>

The Activity is based on the standard template, with the core parts being:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.intro_main); //this loads the xml layout file

// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
Log.i(myApp, "IntroMain:OnCreate");

}

Swapping Logic in the FRE fragment:

public static FREFragment newInstance(int sectionNumber) {
    FREFragment fragment = new FREFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}

public FREFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


    mNum = getArguments().getInt(ARG_SECTION_NUMBER); //figure out which page we're on
    View rootView;

    if (mNum == 1) {
        rootView = inflater.inflate(R.layout.fragment_fre1, container, false);
        Log.i(myApp, "IntroMain:onCreateView:Started:1");
       } else if (mNum == 2) {
        rootView = inflater.inflate(R.layout.fragment_fre2, container, false);
        Log.i(myApp, "IntroMain:onCreateView:Started:2");
       } else {
        rootView = inflater.inflate(R.layout.fragment_fre3, container, false);
        Log.i(myApp, "IntroMain:onCreateView:Started:3");

}

return rootView;
}
Filip
  • 81
  • 1
  • 5
  • I don't know if it's exactly the same issue, but maybe this could help: http://stackoverflow.com/questions/21291222/huge-byte-in-my-app-after-hprof – nKn Dec 15 '14 at 21:28
  • can you add some code? – DennisVA Dec 15 '14 at 21:53
  • Size on disk means nothing. You are rendering a bitmap in memory. Unless you scale it, a bitmap takes x times y times 4 bytes of memory. Many threads on here on how to load and scale bitmaps. – Simon Dec 15 '14 at 22:48
  • possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – Simon Dec 15 '14 at 22:49
  • @Simon - I'm loading 700px * 800px images, so I don't think using the bitmap factory to sample would be helpful for me. From the other threads the thing that comes to mind is that maybe Android is loading it's drawable resources into my heap. I'll be adding some new images shortly and will by how much the heap grows. If it's not a proportional amount it's likely not a bitmap sampling issue. – Filip Dec 16 '14 at 00:05
  • Are you sure? Each image will take about 2MB unless you scale, and since `Bitmap` is an object, they will be created on the heap. Many devices have a heap limit 0f 16MB so those images could quickly chew through available memory. – Simon Dec 18 '14 at 18:17

0 Answers0