Today I will present some suggestion on how to minimize the amount of RAM while loading large bitmap on Android, in order to avoid the common OutOfMemory error.
New mobile phones and tablets have constantly seen their display to become more capable in term of display resolution. This brings many positive aspects, but for developers, usually a lot of headaches: more capable displays means much more memory required to display images and pictures on them.
An image (a PNG, a JPEG..) is always displayed as a matrix of pixels. Supposing a 32 bit depth color, each pixel is represented as 32 bits, i.e. 4 bytes (R,G,B and alpha component).
On a modern display (for example, let's take the Nexus 5), we have 1080x1920 pixels. Potentially, an app that shows an image fullscreen (for simplicity we pretend the black bar with status bar and Android system buttons is hidden) must load in memory a bitmap: 4 * 1080 * 1920 = 8294400 bytes big.
This would not be a problem if the only thing loaded in the current instance of dalvik VM is that image, but of course in memory we can have much more objects.
How much memory does Android borrow to a single app? It depends. Each Android device might in theory have a different threshold which dictates how much memory is granted to the application. It is declared as a system property and can be read by typing this on the console:
adb pull /system/build.prop
cat build.prop
and checking the property dalvik.vm.heapgrowthlimit
In general, devices with bigger dpi screens have a higher value of heap limit. For example, the LG Nexus 5, a XXHDPI device, has 192MB of limit. As said, device with the same resolution might have different heap limit: an HTC One X (XHDPI device) has 92MB of limit; a Samsung S3 (also XHDPI) has only 64MB of heap limit.
An app can request more RAM, by declaring the android:largeHeap property in the Android manifest. How much RAM can be granted? Also this property is defined in the same file (dalvik.vm.heapsize), and in case of my Nexus 5, is 512 MB.
Why not just using always this property? Because the GC tends to become inefficient and app will be much slower.
Consequently, in mostly all cases, instead of setting this attribute we need to minimize the amount of RAM needed, especially if we want to tackle the well known OutOfMemory exception.
In order to minimize the possibility of such error, a well-behaving app that need to load a large bitmap must:
- check the amount of free memory left
- adopt strategies in case the available memory is not enough to load it.
Check the amount of free memory left
Android inherits the Runtime clas sfrom Java standard library. This helps getting the information we fetched via console: in particular, how much memory is available, how much free memory is left before the VM will complain.
Check how big is the bitmap we want to load
On Android3.0+, we can use a handy class called BitmapFactory.Options. It does a lot of useful things, one of which is giving info about the image, without loading it in memory.In the snippet above, I use the aforementioned class to calculate image width and height without loading it in RAM, by setting to true the inJustDecodeBounds property. We can have a rough aestimate of how large will be the image in RAM from its width, height attributes.
Load a subscaled version of the bitmap in RAM
If the memory available is less than the amount of memory required by the bitmap, then we apply some subsampling. The image will be imperceptibly lower in quality on the specific device where this subsampling will happen, but we will avoid the Out of Memory error. User will be happy and probably won't even notice the difference.
Again, to subsample the image we use the BitmapFactory.Options:
By subsampling an image with a factor of 2 you can see you will be able to spare almost 75% of memory, and the difference in quality is almost negligible.
You can download the sample app which contains all these snippets and a demo activity you can play with here: https://github.com/nalitzis/TestMemoryViews
Your posts is really helpful for me.Thanks for your wonderful post. I am very happy to read your post. Phonegap training in chennai | Phonegap training chennai | Phonegap course in chennai | Phonegap course chennai
ReplyDeletehi body! as I know , the amount of free memory left is alway not accurate and unstable, had you ever try this in develop? I doubt that it is ever work?
ReplyDeleteThe information you have given here are most worthy for me. I have implemented in my training program as well, thanks for sharing.
ReplyDeleteHadoop Training Chennai
Hadoop Training in Chennai
I have read your blog, it was good to read & I am getting some useful info's through your blog keep sharing... Informatica is an ETL tools helps to transform your old business leads into new vision. Learn Informatica training in chennaifrom corporate professionals with very good experience in informatica tool.
ReplyDeleteRegards,
Best Informatica Training In Chennai|Informatica training center in Chennai
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteIt is really very helpful for us and I have gathered some important information from this blog.If anyone wants to Selenium Training in Chennai reach Greens Technology training and placement academy.
ReplyDeleteselenium Training in Chennai
This is a great post, very interesting points and I have enjoyed immeasurably. thanks!
ReplyDeleteSEO services Melbourne | Magento Development Company
Thanks Admin for sharing such a useful post, I hope it’s useful to many individuals for developing their skill to get good career.
ReplyDeleteRegards,
SAP training in chennai|SAP course in chennai|SAP Training in Chennai|sap course in Chennai
The article you have shared is impressive and I am glad to read this posting. Thank you so much for sharing it
ReplyDeletewebsite design Melbourne | custom website design services |SEO services Melbourne | SMO services Melbourne
Appreciated work. I like this post a lot. I hope it will be helpful for all of the readers.Keep it up..
ReplyDeletecustom web development Melbourne | Magento Development Company | mobile application development melbourne | ipad application development melbourne
Awesome Post!!! With unique content, I really get reading interest when I am following your article, I hope I ll help many of them who looking this pretty information.
ReplyDeleteRegards,
ccna course in Chennai|ccna training in Chennai
Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
ReplyDeleteqtp training in chennai
good posting.....
ReplyDeletesas training in chennai
nice blogs.....
ReplyDeleteunix training in chennai
thanks so much
ReplyDeletemicrostrategy training in chennai
All of these tips are great, that’s very interesting. I’m so tempted to try that myself, but you would think if it were effective, more people would do it.
ReplyDeleteiOS training in chennai
ReplyDeleteWe share it this site's information very advantages. I thinking many interesting here.
This information is impressive; I am inspired by your post writing style & how continuously you describe this topic.
Java Training in Chennai
Java Training Institute in Chennai
ReplyDeleteIncredible post. Articles that have significant and savvy remarks are more agreeable, at any rate to me. It’s fascinating to peruse what other individuals thought and how it identifies with them or their customers, as their point of view could help you later on.
PSD to Wordpress
wordpress website development
Its really nice article and informative.Thanks for sharing this article.
ReplyDeleteiOS Training In Chennai | iOS Training Institute In Chennai
After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
ReplyDeleteMEAN stack training in annanagar
MEAN stack training in Velachery
MEAN stack training Sholinganallur
Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every blog.i am expecting more updated posts from your hands.
ReplyDeleteSap Pm Training From India
Sap Mm Training From India
Sap Sd Training From India
I think this is the best article today about the future technology. Thanks for taking your own time to discuss this topic, I feel happy about that curiosity has increased to learn more about this topic. Keep sharing your information regularly for my future reference.
ReplyDeleteSelenium Training in Chennai
software testing selenium training
JAVA J2EE Training Institutes in Chennai
JAVA Course
iOS Training in Adyar
iOS Course in Tambaram
Awwsome informative blog ,Very good information thanks for sharing such wonderful blog with us ,after long time came across such knowlegeble blog. keep sharing such informative blog with us.
ReplyDeleteAviation Academy in Chennai | Aviation Courses in Chennai | Best Aviation Academy in Chennai | Aviation Institute in Chennai | Aviation Training in Chennai
Nice post. I learned some new information. Thanks for sharing.
ReplyDeleteTechnology
redbeardpress
Nice idea,keep sharing your ideas with us.i hope this information's will be helpful for the new learners.
ReplyDeleteJava Training in Sholinganallur
Java Training in T nagar
Java Training in Anna nagar
Java Training in Bangalore
Thank you for sharing this wonderful information with us. Keep up the good work.
ReplyDeleteBest Placement Training institutes in Chennai | Training and job placement in Chennai | Training come placement in Chennai | Placement courses in Chennai | Placement courses in Velachery | Best Placement Training institutes in Tambaram
Your article gives lots of information to me. I really appreciate your efforts admin, continue sharing more like this.
ReplyDeleteAzure course Chennai
Best Azure Training institute in Chennai
Best Azure Training center in Chennai
Azure course in Tambaram
DevOps Certification Chennai
UiPath Training in Chennai
Machine Learning Course in Chennai
Really it was an awesome article. very interesting to read.
ReplyDeleteThanks for sharing.
Tableau Training in Chennai
Tableau Course in Chennai
Tableau Certification in Chennai
Tableau Training Institutes in Chennai
Tableau Certification
Tableau Training
Tableau Course
Very nice post here thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's. Machine learning training in chennai
ReplyDeletepython machine learning training in chennai
Great efforts put it to find the list of articles which is very useful to know, Definitely will share the same to other forums.| Roles and reponsibilities of hadoop developer | hadoop developer skills Set | hadoop training course fees in chennai | Hadoop Training in Chennai Omr
ReplyDeleteAwesome Post. It was a pleasure reading your article. Thanks for sharing.
ReplyDeletePega training institutes
Pega training courses
Pega administrator training
Pega testing training
Pega software training
Pega software course
ReplyDeleteI have read so many articles and definitely this one is the best I have read. Thanks for uploading.
selenium Training in Chennai
Selenium Training Chennai
ios training institute in chennai
Digital Marketing Course in Chennai
.Net coaching centre in chennai
Big Data Training in Chennai
French Classes in Chennai
Amazing Post. Great write-up. Extra-ordinary work. Waiting for your next Post.
ReplyDeleteSocial Media Marketing Courses in Chennai
Social Media Marketing Training in Chennai
Social Media Training in Chennai
Social Media Marketing Training
Social Media Marketing Courses
Social Media Training
Social Media Marketing Training
Social Media Courses
Nice idea,keep sharing your ideas with us.i hope this information's will be helpful for the new learners.
ReplyDeleteangularjs classes in bangalore
angularjs tutorial in bangalore
AngularJS Course in Anna Nagar
AngularJS Courses in T nagar
This comment has been removed by the author.
ReplyDeleteVery nice post keep on posting
ReplyDeleteBest Machine learning training in chennai
Good post very interesting to read
ReplyDeleteAb Initio training in chennai
Well said.
ReplyDeleteAuthorized iphone service center in Chennai | iphone service center in chennai | Mobile service center in chennai | Authorized iphone service center in Chennai | iphone service center in chennai | Authorized iphone service center in Chennai | iphone battery replacement
ok
ReplyDeletePhối chó bull pháp
Phối giống chó Corgi
Phối chó Pug
chó Poodle
chó Poodle giá bao nhiêu
Máte skvelý článok. Prajem vám produktívny deň
ReplyDeletebon ngam chan
máy ngâm chân giải độc
bồn mát xa chân
chậu ngâm chân giá rẻ
Vanskeligheter( van bi ) vil passere. På samme måte som( van điện từ ) regnet utenfor( van giảm áp ) vinduet, hvor nostalgisk( van xả khí ) er det som til slutt( van cửa ) vil fjerne( van công nghiệp ) himmelen.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteImages in PNG format are also a great thing for bitmaps. People can convert jpg to supplier png: https://jpg4png.com/ for the most practical experience
ReplyDeleteThis comment has been removed by the author.
ReplyDelete
ReplyDeleteclick here formore info.
............................................................
Really i am Enjoy Reading all the Articles...Thanks for Such an Interesting Information's and waiting to read many more Articles like this and visit below for more...
ReplyDeleteJava training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery
Truly welcome this magnificent post that you have accommodated us. Great site and an incredible theme to I truly get astonished to understand this. It's great. Thank you! startup web design
ReplyDeleteI am really enjoying reading your well written articles. It’s hard to come by experienced people about this subject, but you seem like you know what you’re talking about! Thanks.
ReplyDeleteJava Training in Chennai
Java Training in Velachery
Java Training inTambaram
Java Training in Porur
Java Training in Omr
Java Training in Annanagar
This comment has been removed by the author.
ReplyDeleteThanks for sharing informative article on java application development. Your post helped to understand the career in Java
ReplyDeleteDigital Marketing Training in Chennai
Digital Marketing Training in Velachery
Digital Marketing Training in Tambaram
Digital Marketing Training in Porur
Digital Marketing Training in Omr
Digital MarketingTraining in Annanagar
I have read so many articles and definitely this one is the best I have read. Thanks for uploading.
ReplyDeleteSoftware Testing Training in Chennai
Software Testing Training in Velachery
Software Testing Training in Tambaram
Software Testing Training in Porur
Software Testing Training in Omr
Software Testing Training in Annanagar
very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
ReplyDeleteDevOps Training in Chennai
DevOps Course in Chennai
thanks for giving great article...
ReplyDeleteInternet of Things(iot) in coimbatore
IOT Training in coimbatore
Top 10 IOT training institute in coimbatore
Internet of Things(iot) training course in coimbatore
Internet of Things(IOT) course in coimbatore
Best iot training in coimbatore
iot application training in coimbatore
iot based projects in coimbatore
iot online training in coimbatore
iot training near me
Best iot training institute in coimbatore
Internet of Things(iot) course in coimbatore
Internet of Things course training in coimbatore
Here is the best Software training institute to study AWS training in Chennai, from Infycle Technologies and we come up with courses like Cyber Security, Graphic Design and Animation, Block Security, Java, Cyber Security, Oracle, Python, Big data, Azure, Python, Manual and Automation Testing, DevOps, Medical Coding etc., with outstanding training with experienced trainers and a friendly environment with 100+ Live Practical Sessions and Real-Time scenarios. After completion of the course a student will be able to obtain more knowledge of the course and be able to crack interviews on top MNC’s. For more queries dial 7504633633 or 7502633633.
ReplyDeleteExcellent Post,Thanks for sharing really interesting and informative content. with some great tool recommendation.
ReplyDeletewe are the Top Web Development Company in Bangalore
This comment has been removed by the author.
ReplyDelete