Autoboxing, a little thing, can cause big problems for Android App Performance
To ensure your app performs well across a wide variety of
devices, you always try to use best practices. On that front memory optimization
is often a demanding task. Most of the time we always think on the bigger side
of it and miss the small thing that can add up to a big problem. Autoboxing is
a topic that often goes unnoticed when talking about performance issues.
Autoboxing is a great language feature, until you realize how
much of your performance is being lost to its convenience. You cannot imagine a
simple Autoboxing operations can easily tear through your Application’s memory.
What is
Autoboxing?
Till JDK 1.4, when we work with primitive types, it requires
repetitive work of converting the primitive types into wrapper classes and
vice-versa. Let’s say if we want to create a List with Integers, we have to
explicitly wrap them as Integer and we need to unwrap it to get int value.
So since JDK 1.5, Conversion from primitive types to
corresponding wrapper objects and vice versa can happen automatically.
Boxing: Conversion
of a primitive type to the corresponding reference type (int -> Integer).
Unboxing:
Conversion
of the reference type to the corresponding primitive type (Integer -> int).
How it
causes problem?
As we discussed Java provides corresponding wrapper classes
that match primitive types, and act as object wrappers. These classes give the
same functionalities as primitives but can be used with generic collections.
However their size in memory is not the same. An Integer object occupies 4
times more memory as primitive int.
In the example below, the API call adding a primitive int
value but the collection API takes Integer object. This assignment will perform
an Autoboxing operation from int to Integer object.
// Here is primitive variable
int value = 5;
List<Integer>
list = new ArrayList<>();
//autoboxing
to add primitive type (int) in collection class
list.add(value);
For a single call, this may not matter much. However, if we
are using it frequently, for example in an inner loop, it might occupy a lot of
memory unnecessarily and cause a big memory threat.
// Here is generic variable
Integer sum = 0;
for(int i= 0;i< 100;i++){
// autoboxing to
convert int into Integer
sum = sum + i;
}
As you can see in the above example, 100 new objects
allocation along with the sum calculation. But in case of primitive it doesn’t
require any additional allocation overhead.
This will also overhead
of more garbage collection and decrease the runtime performance. It can be
especially painful with data structures like HashMap. Let’s consider another code:
HashMap<Integer, String> map = new HashMap< >();
// Put the value in the map where key is primitive type.
map.put(1, "TestValue"); // expected key is Integer
type
// Get the value from the map where key is primitive type.
map.get(1); // expected key is Integer type
In the above code put and get operations will use Autoboxing
and cause additional load. Basically anytime you insert, update or retrieve a
value with this generic container when a primitive is involved you end up
boxing or unboxing values.
How to resolve
the problem?
To resolve the problem of autoboxing Android provided custom
containers SparseArrays and ArrayMap to use instead of traditional containers
like HashMap. These containers are design specifically to fight Autoboxing
problem and eliminate both runtime overhead and additional memory allocation. These
containers are more memory efficient than using a HashMap to map Integers to
Objects.
Below are the example code to create SparseArray:
SparseArray sparseArray = new SparseArray();
sparseArray.put(1, “Value1”);
SparseLongArray sparseLongArray = new SparseLongArray();
sparseLongArray.put(1, 1L);
SparseBooleanArray sparseBooleanArray = new
SparseBooleanArray();
sparseBooleanArray.put(1, true);
SparseIntArray sparseIntArray = new SparseIntArray();
sparseIntArray.put(1, 2);
This way you make your code more optimized and memory
efficient. To understand SparseArray and ArrayMap refer my detailed post.
How to
trace memory issues?
Android Studio provides profiling tools to records and compute
memory allocations of your app. Allocation Tracker are such a powerful tools
which shows object allocation and their size. It is also used to track memory
leaks.
Another good tool is Traceview, which is a graphical viewer
for execution logs that you create by using the Debug class to log tracing
information in your code
Conclusion
Now a day’s most of the applications are handling thousands of
data, so any optimization you make could potentially improve the performance
across our application. Small change of Autoboxing matters a lot for a mobile
developer where you have very limited memory and resources.
To find more interesting topics on Software development follow
me at:
This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
ReplyDeleteAndroid Online Training
wow A Wonderful website I like this blog very much.This blog can help lot of user to update their knowledge.Thanks for sharing this kind of blog.Anybody want to learn
ReplyDeletehttp://www.traininginannanagar.in/android-training-in-chennai.html
wow A Wonderful website I like this blog very much.This blog can help lot of user to update their knowledge.Thanks for sharing this kind of blog.Anybody want to learn
ReplyDeleteAndroid Training in Chennai
You have post an very useful blog about android. Its very interesting and useful to me and i have learn lots of new information from your blog share. keep update such kind of interesting stuff. thank you...
ReplyDeleteAndroid Training in Chennai
Your blog have a very nice informative content on Android Online Training
ReplyDeleteHi, your blog really nice, keep it up! I’ll go ahead and bookmark your website to come back later.
ReplyDeleteRegards,
Java Online Training | Java Online Training in India | Java Online Training India | Java Online Training in Hyderabad | Java Online Training Hyderabad | Java Training in Hyderabad | Java Training in India | Java Training Institutes in India | Java Training Institutes in Hyderabad | Java Course in Hyderabad | Java Training | Learn Java Online | Online Java Training | Best Java online Training Institutes in Hyderabad | Best Java Training Institutes in Hyderabad | Best Institutes for Java | Java Institutes in Hyderabad | Best Institutes for Java in Hyderabad | Learn Java | Java Training Institutes in Hyderabad | Java Certification | Java Certification Training | Java Certification Training in Hyderabad | Java Certification Training in India
Check it Through Android Online Training For more info.
ReplyDeleteNice Information Android Online Training, keep share
ReplyDeleteThe information related to the autoboxing is really informative... Wish to learn Android from the basics to the advanced level, enroll you in the Best Software Training Institute in Velachery to learn quickly...
ReplyDeleteAndroid Training Institute in Velachery | Best Android Training Institute in Velachery
Nice to read this article... Thanks for sharing.......
ReplyDeleteAndroid training
Nice information, check it once through Android Online Training for more info.
ReplyDelete
ReplyDeleteNice good info Android Online Training Hyderabad
Thanks for your article on Android technology. Android is an open source platform that allows developers to create stunning website loaded with various advanced features and functionalities. Android Training in Chennai | Android Course in Chennai
ReplyDeleteThank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read.
ReplyDeleteAndroid Training in Chennai
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this. Android Training in Chennai
ReplyDeleteAndroid Training Company Delhi | Android Trainer in Delhi
ReplyDeleteThanks for above information, I like it. check it once Android Online Training
ReplyDeletenice blog
ReplyDeletehadoop training in chennai
The information which you have provided is very good. It is very useful who is looking for selenium Online Training Bangalore
ReplyDeleteThis is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
ReplyDeleteAndroid Training In Chennai
Nice blog..! I really loved reading through this article. Thanks for sharing such
ReplyDeletean amazing post with us and keep blogging... RPA Training in Chennai | Blue Prism Training in Chennai
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletePython Training in electronic city
DataScience with Python Training in electronic city
AWS Training in electronic city
Big Data Hadoop Training in electronic city
Devops Training in electronic city
React JS Training in electronic city
Selenium training in electronic city
core Java training in electronic city
ReplyDeleteNice blog..! I really loved reading through this article. Thanks for sharing such a amazing post with us and keep blogging...
Best Data Science online training in Hyderabad
Data Science training in Hyderabad
Data Science online training in Hyderabad
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
ReplyDeleteOnline DevOps Certification Course - Gangboard
Best Devops Training institute in Chennai
Really you have done great job,There are may person searching about that now they will find enough resources by your post
ReplyDeleteBest Devops training in sholinganallur
Devops training in velachery
Devops training in annanagar
Devops training in tambaram
Amazing blog about the various informative information on the programming languages... Java Training in Chennai | Pega Training in Chennai
ReplyDeleteHowdy, would you mind letting me know which web host you’re utilizing? I’ve loaded your blog in 3 completely different web browsers, and I must say this blog loads a lot quicker than most. Can you suggest a good internet hosting provider at a reasonable price?
ReplyDeleteBest AWS Amazon Web Services Training in Chennai | Best AWS Training and Certification for Solution Architect in Chennai
Amazon Web Services Training in Tambaram, Chennai|Best AWS Training in Tambaram, Chennai
It would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.
ReplyDeleteJava interview questions and answers
Core Java interview questions and answers
Java training in Chennai | Java training in Tambaram
Java training in Chennai | Java training in Velachery
Thanks for splitting your comprehension with us. It’s really useful to me & I hope it helps the people who in need of this vital information.
ReplyDeletepython training institute in marathahalli
python training institute in btm
Python training course in Chennai
Nice blog. Thanks for explaining the Android concept, Autoboxing in detail. Keep sharing the latest updates about Android.
ReplyDeleteAndroid Application Development company in Coimbatore
iOS Application Development Company in Coimbatore
I am really impressed with your efforts and really pleased to visit this post.
ReplyDeleteData Science training in Chennai | Data Science Training Institute in Chennai
Data science training in Bangalore | Data Science Training institute in Bangalore
Data science training in pune | Data Science training institute in Pune
Data science online training | online Data Science certification Training-Gangboard
Data Science Interview questions and answers
Were a gaggle of volunteers as well as starting off a brand new gumption within a community. Your blog furnished us precious details to be effective on. You've got completed any amazing work!
ReplyDeleteJava training in Chennai | Java training institute in Chennai | Java course in Chennai
Java training in Bangalore | Java training in Electronic city
Java training in Bangalore | Java training in Marathahalli
Java training in Bangalore | Java training in Btm layout
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
ReplyDeleterpa training in chennai
Best rpa training in bangalore
rpa course in bangalore
rpa training in marathahalli
rpa training in btm
best rpa training in chennai
Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.
ReplyDeleteAmazon web services Online Training |Best Online AWS Certification Course
AWS Training in Chennai | Advanced Amazon Web Services Training Institute in Chennai
AWS Training in Bangalore |Best AWS Training in Bangalore
Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.
ReplyDeleteAWS Online Training |Best Online AWS Certification Course
AWS Training in Chennai | Advanced Amazon Web Services Training Institute in Chennai
AWS Training in Bangalore |Best AWS Training Institute in Bangalore
Read all the information that i've given in above article. It'll give u the whole idea about it.
ReplyDeleteBest Devops online Training
Online DevOps Certification Course - Gangboard
This pretty much applies to every job profile. Dissatisfaction , politics and people expecting you to know and do everything . I believe job positions that are extremely close to business side of things will be more rewarding . Decisions you take and mistakes you make must have the shortest path to revenue impacts , That is a place where you will be respected.
ReplyDeleteJava training in Chennai | Java training institute in Chennai | Java course in Chennai
Thank you for benefiting from time to focus on this kind of, I feel firmly about it and also really like comprehending far more with this particular subject matter. In case doable, when you get know-how, is it possible to thoughts modernizing your site together with far more details? It’s extremely useful to me
ReplyDeletePython Online certification training
python Training institute in Chennai
Python training institute in Bangalore
Amazing Article ! I have bookmarked this article page as i received good information from this. All the best for the upcoming articles. I will be waiting for your new articles. Thank You ! Kindly Visit Us @ Coimbatore Travels | Ooty Travels | Coimbatore Airport Taxi
ReplyDeleteThe given information was excellent and useful. This is one of the excellent blog, I have come across. Do share more.
ReplyDeleteDevOps course in Chennai
Best DevOps Training in Chennai
Amazon web services Training in Chennai
AWS Certification in Chennai
Data Analytics Courses in Chennai
Big Data Analytics Courses in Chennai
DevOps Training in Anna Nagar
DevOps Training in T Nagar
Information from this blog is very useful for me, am very happy to read this blog Kindly visit us @ Luxury Watch Box | Shoe Box Manufacturer | Candle Packaging Boxes
ReplyDeleteWow...What an excellent informative blog, really helpful. Thank you so much for sharing such a wonderful article with us.keep updating..
ReplyDeletesalesforce Training in Bangalore
uipath Training in Bangalore
blueprism Training in Bangalore
Your post is just outstanding! thanx for such a post,its really going great and great work.
ReplyDeletepython training in kalyan nagar|python training in marathahalli
selenium training in marathahalli|selenium training in bangalore
devops training in kalyan nagar|devops training in bellandur
phthon training in bangalore
nice blog.keep on going.
ReplyDeletedata science course in Hyderabad with placements
very interesting informative post.Thanks you so much for the post and the tips for users.I love the post a lot and intreseted to know more about it
ReplyDeleteamazon webservices training institute in pune
The information provided is of great use as I got to learn new things. Keep Blogging.Best Android Training Institute
ReplyDeleteNice blog keep going..
ReplyDeleteSAP R3 services in India
SAP Hana services in India
PHP Development Services in India
Mobile Application Software Services in India
JAVA Development Services in India
SAP B1 services in India
Thanks for sharing the awesome post.
ReplyDeleteAndroid App Development Company Malaysia
Thanks for your post! Through your pen I found the problem up interesting! I believe there are many other people who are interested in them just like me! Thanks your shared!... I hope you will continue to have similar posts to share with everyone! I believe a lot of people will be surprised to read this article! DevOps online training in hyderabad
ReplyDeleteHello Admin!
ReplyDeleteThanks for the post. It was very interesting and meaningful. I really appreciate it! Keep updating stuffs like this. If you are looking for the Advertising Agency in Chennai | Printing in Chennai , Visit Inoventic Creative Agency Today..
Wow finally I got good article about android development process from beginning. Thanks for sharing
ReplyDeleteAndroid App Development Company Malaysia
Custom Software Development Company Malaysia
Software Development Company Malaysia
I have recently found an excellent Salesforce Training in India whose faculty is exceptional and you can also try this Salesforce Training and Certification in Jaipur whose syllabus is state of art. Here at Salesforce Training in Mumbai instructors are perfect to teach salesforce crm. My advice for you is to join demo at Salesforce training in Pune | Course Cost and in weekends try this best Salesforce Training in Noida | Course Cost who is providing great teaching services on Salesforce Training in Delhi and Fee Details.
ReplyDeleteI think you did an awesome allahabad university BA 3rd year result job explaining it. Sure beats having to research it allahabad university bsc 1st year Result on my own. Thanks
ReplyDeleteabinitio training
ReplyDeletespark training
scala training
azure devops training
app v training
sccm training
Nice read, I just passed this onto a friend who was doing a little research on that. Jungle Cruise Full Movie 2021
ReplyDeleteWriting this type of article is one of the most important things you will ever learn. This is the type of articleBuy tinder accounts you will use to build your reputation as an author and also one that will help you get started in a career that is very profitable. Many people will ask me what I consider to be the most important part of this type of article. The answer is simple, the title. You need to create a quality title for your article before submitting it for publication.Buy snapchat account
ReplyDeleteAgen Slot Online Terpercaya
ReplyDeleteAgen Slot Online Terpercaya
sap sd online training
ReplyDeletesap bw on hana online training
windows admin online training
app v online training
osb online training
oracle scm online training
I really like and appreciate your post.Really thank you! Fantastic.
ReplyDeleteVisit us: Core Java Online Training India
Visit us: Core java online training Hyderabad
If you’re a business owner looking for bulk PVA Accounts to boost your marketing efforts, the right agency can be a major asset. buypvaaccounts.xyz is the best bulk PVA Accounts provider agency in the market. Buy PVA Accounts with premium quality at an affordable price.
ReplyDeleteBuy PVA Accounts
Buy Gmail Accounts
Buy Facebook Accounts
Buy Instagram Accounts
Thanks a lot for sharing us about this update. Hope you will not get tired on making posts as informative as this.
ReplyDeleteBuy Gmail Accounts
Buy Email Account
nice and useful information
ReplyDeleteBest Website Development and Hosting in Hyderabad
Great Blog!
ReplyDeleteI appreciate the depth of knowledge you bring to your writing. Thanks for sharing. If you want to hire dedicated Android app developers for the project. Join us!
This comment has been removed by the author.
ReplyDelete