4

My application is running fine on tablets its taking memory very less about 20-30 MB as can be seen DBMS -> Debug & Heap tracker. But when running same application on Devices like Samsung Galaxy Note-4 (2560 x 1440) & LG G3 (2392 x 1440) its taking heap space of about 200MB at first activity and then continuously growing.

Heap Size on first activity run

I have tried to check logcats for memory leakage and fixed issues, checked for cursor and database instances closed them too. Tried finishing activity once moved to other one. But still getting "Out Of Memory" error.

Tried increasing heap size to large even but its not helping though, since memory is not getting released somehow.

Note: I am using same drawable images for whole application, no different folders for other devices are created. Whole images size is about 4MB which is having almost 400 images, but only 15 images are used at a time in any layout.

trincot
  • 317,000
  • 35
  • 244
  • 286
TheMohanAhuja
  • 1,855
  • 2
  • 19
  • 30
  • Where does the OutOfMemoryError is thrown? Can you post a stack trace? Are you using some framework for downloading/caching the images? (Like UIL or other)? – Felix Feb 04 '15 at 12:30
  • No I am not downloading or caching images, its in drawable folder only. And OutOfMemory comes on any page when loading new activity but when heap is filled. – TheMohanAhuja Feb 04 '15 at 12:47

2 Answers2

8

I got answer for my problem after reading a lot for continuous 3 days finally my problem is solved.

Well, what's happening is that

setBackgroundResource(R.drawable.imageName)

is going to cause Android to first do the

BitmapFactory.decodeResource()

Which will actually do some resampling based on screen density (i.e., automatic density-based resampling for device resolution) which was causing size to grow much and that too for PNG images.

Android takes almost 10 times more space to render PNG then JPG, so it was consuming much space as my application was rending 10-15 PNG images at any activity. Since android doesn't free up space of memory, if your application is running, resulting OOM (Out Of Memory) error to come up.

So what I have done is just put res/drawable to res/drawable-nodpi/ (to prevent automatic density-based resampling) and I am sorted.

Hope this helps someone else too.

Reference link:

Android background image memory usage

Community
  • 1
  • 1
TheMohanAhuja
  • 1,855
  • 2
  • 19
  • 30
  • Very good find, I had no idea. I set the drawable folder to drawable-nodpi and I noticed a huge increase in the speed of loading multiple fragments in a bottom bar (with all the images and everything that comes with it). Thanks! – Odaym Feb 25 '17 at 15:27
  • @odaym, I am Glad it helped. – TheMohanAhuja Mar 01 '17 at 08:54
0

Maybe you increase heap size incorrectly. In AndroidManifest put:

 android:largeHeap="true"

If it doesnt work, try to use this inbuild.settings` file:

settings ={ android = { largeHeap = true }}

If it doesn`t work, try to:

<android xmlns:android="http://schemas.android.com/apk/res/android">
        <tool-api-level>14</tool-api-level>
        <manifest android:installLocation="auto" android:versionCode="1"
            android:versionName="1" package="com.company.appname" xmlns:android="http://schemas.android.com/apk/res/android">
            <uses-sdk android:minSdkVersion="14"/>

And then

<application android:debuggable="false"
    android:icon="@drawable/appicon"
    android:label="Appname" android:largeHeap="true" android:name="AppnameApplication">

Also you can increase heap size programmaticaly:

Check for ApplicationInfo objects with FLAG_LARGE_HEAP. You can get these objects from PackageManager (e.g., via getApplicationInfo()).

GIGAMOLE
  • 1,274
  • 1
  • 11
  • 17