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
Thank 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
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
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
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
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
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
The 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
Wow...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
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
ReplyDeleteThanks for sharing the awesome post.
ReplyDeleteAndroid App Development Company Malaysia
I 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
ReplyDeleteNice 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
ReplyDeletenice 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