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:

Medium      Twitter     Google+

Comments

  1. 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.

    Android Online Training

    ReplyDelete
  2. 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
    http://www.traininginannanagar.in/android-training-in-chennai.html

    ReplyDelete
  3. 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

    Android Training in Chennai

    ReplyDelete
  4. 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...
    Android Training in Chennai

    ReplyDelete
  5. Your blog have a very nice informative content on Android Online Training

    ReplyDelete
  6. The 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...
    Android Training Institute in Velachery | Best Android Training Institute in Velachery

    ReplyDelete
  7. Nice to read this article... Thanks for sharing.......
    Android training

    ReplyDelete
  8. Nice information, check it once through Android Online Training for more info.

    ReplyDelete
  9. 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

    ReplyDelete
  10. 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.
    Android Training in Chennai

    ReplyDelete
  11. 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

    ReplyDelete
  12. Thanks for above information, I like it. check it once Android Online Training

    ReplyDelete
  13. The information which you have provided is very good. It is very useful who is looking for Java online training Hyderabad

    ReplyDelete
  14. Thanks for sharing such a great information..Its really nice and informative..
    Best Software Training Centre in Chennai | Software Training Centre in Chennai

    ReplyDelete
  15. The information which you have provided is very good. It is very useful who is looking for selenium Online Training Bangalore

    ReplyDelete
  16. 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.
    Android Training In Chennai

    ReplyDelete
  17. Those rules moreover attempted to wind up plainly a decent approach to perceive that other individuals online have the indistinguishable enthusiasm like mine to get a handle on incredible arrangement more around this condition.
    fire and safety course in chennai

    ReplyDelete
  18. Nice blog..! I really loved reading through this article. Thanks for sharing such
    an amazing post with us and keep blogging... RPA Training in Chennai | Blue Prism Training in Chennai

    ReplyDelete
  19. This is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.
    apple ios training institutes in Hyderabad
    ios app development in hyderabad

    ReplyDelete
  20. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site. RPA Training in Chennai | Blue Prism Training in Chennai

    ReplyDelete
  21. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your blog.
    iPhone training courses in bangalore
    ios app development course

    ReplyDelete

  22. Nice 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

    ReplyDelete
  23. myTectra Placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance

    ReplyDelete
  24. 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.
    Online DevOps Certification Course - Gangboard
    Best Devops Training institute in Chennai

    ReplyDelete
  25. Really you have done great job,There are may person searching about that now they will find enough resources by your post
    Best Devops training in sholinganallur
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram

    ReplyDelete
  26. Amazing blog about the various informative information on the programming languages... Java Training in Chennai | Pega Training in Chennai

    ReplyDelete
  27. Howdy, 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?
    Best 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

    ReplyDelete
  28. 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.
    Java 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

    ReplyDelete
  29. 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. 
    python training institute in marathahalli
    python training institute in btm
    Python training course in Chennai

    ReplyDelete
  30. Nice blog. Thanks for explaining the Android concept, Autoboxing in detail. Keep sharing the latest updates about Android.
    Android Application Development company in Coimbatore
    iOS Application Development Company in Coimbatore

    ReplyDelete
  31. Well researched article and I appreciate this. The blog is subscribed and will see new topics soon.
    python course in pune
    python course in chennai
    python Training in Bangalore

    ReplyDelete
  32. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    best rpa training in chennai
    rpa training in chennai
    rpa interview questions and answers
    automation anywhere interview questions and answers
    blueprism interview questions and answers
    uipath interview questions and answers
    rpa training in bangalore

    ReplyDelete
  33. 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.

    rpa 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

    ReplyDelete
  34. 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.

    Amazon 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

    ReplyDelete
  35. 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.

    Amazon 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

    ReplyDelete
  36. 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.

    AWS 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

    ReplyDelete
  37. 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.

    AWS 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

    ReplyDelete
  38. Read all the information that i've given in above article. It'll give u the whole idea about it.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

    ReplyDelete
  39. 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.
    Java training in Chennai | Java training institute in Chennai | Java course in Chennai

    ReplyDelete
  40. 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 
    Python Online certification training
    python Training institute in Chennai
    Python training institute in Bangalore

    ReplyDelete
  41. 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

    ReplyDelete

  42. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
    aws training in bangalore
    RPA Training in bangalore
    Python Training in bangalore
    Selenium Training in bangalore
    Hadoop Training in bangalore

    ReplyDelete
  43. Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. Such a nice blog you are providing ! Kindly Visit Us @ Best Travels in Madurai | Tours and Travels in Madurai | Madurai Travels

    ReplyDelete
  44. Thanks For Sharing The Information The Information Shared Is Very Valuable Please Keep Updating Us Time Just Went On Reading The article Python Online Course Hadoop Online Course Aws Online Course Data Science Online Course

    ReplyDelete
  45. 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

    ReplyDelete
  46. Wow...What an excellent informative blog, really helpful. Thank you so much for sharing such a wonderful article with us.keep updating..
    salesforce Training in Bangalore
    uipath Training in Bangalore
    blueprism Training in Bangalore

    ReplyDelete
  47. 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
    amazon webservices training institute in pune

    ReplyDelete
  48. The information provided is of great use as I got to learn new things. Keep Blogging.Best Android Training Institute


    ReplyDelete
  49. Today, the future is in the hands of cross-platform app frameworks. Cross-platform app development frameworks allow developers to create mobile applications that are compatible with more than one operating systems, in this case, iOS and Android. Here you find, what are the vital reasons you should learn 7 programming languages in 2020?

    ReplyDelete
  50. 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


    ReplyDelete
  51. Hello Admin!

    Thanks 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..

    ReplyDelete
  52. 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.

    ReplyDelete
  53. 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

    ReplyDelete
  54. Nice read, I just passed this onto a friend who was doing a little research on that. Jungle Cruise Full Movie 2021

    ReplyDelete
  55. Writing 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

    ReplyDelete
  56. I really like and appreciate your post.Really thank you! Fantastic.
    Visit us: Core Java Online Training India
    Visit us: Core java online training Hyderabad

    ReplyDelete
  57. 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.
    Buy PVA Accounts
    Buy Gmail Accounts
    Buy Facebook Accounts
    Buy Instagram Accounts

    ReplyDelete
  58. Thanks a lot for sharing us about this update. Hope you will not get tired on making posts as informative as this.
    Buy Gmail Accounts
    Buy Email Account

    ReplyDelete
  59. Ios app development Bangalore
    Apprient is the most preferred iOS app Development Company in Bangalore. with hundreds of apps delivered across a wide range of business domains over.

    ReplyDelete
  60. Are you looking for the best Azure Training in Chennai? Here is the best suggestion for you, Infycle Technologies the best Software training institute in Chennai to study Azure platform with the top demanding courses such as Graphic Design and Animation, Cyber Security, Blockchain, Data Science, Oracle, AWS DevOps, Python, Big data, Python, Selenium Testing, Medical Coding, etc., with best offers. To know more about the offers, approach us on +91-7504633633, +91-7502633633

    ReplyDelete
  61. Snapchat is a well-known internet messaging application buy snapchat account available for Android and iOS devices. Not only can you share photographs and videos with your friends instantly, but you can also engage in a variety of activities with the verified snapchat account.

    ReplyDelete

Post a Comment

Popular posts from this blog

Android Performance: Avoid using ENUM on Android

Secure and smaller APK size using Proguard

Local Broadcast, less overhead and secure in Android