Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of Memory Error on High Resolution mobile phones

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.

like image 820
TheMohanAhuja Avatar asked Feb 11 '23 00:02

TheMohanAhuja


1 Answers

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

like image 87
TheMohanAhuja Avatar answered Feb 13 '23 15:02

TheMohanAhuja