Android memory management: screen density, required image sizes and available heap

Guess what else is the Android-Bitmap-OOM question!

Background

While stress testing our application , it was noted that it is possible to maximize the memory allocation of the application after prolonged, intensive use (monkey runner) with OutOfMemory exceptions are written in the next stack. The application loads images (about 3 at a time) when a page is selected under ViewPager . You can download more than 280 images for download when the length and breath of the application are implemented. The app uses Picasso by Square to download an abstraction image. It is noteworthy that in no case do we manipulate Bitmaps directly in our application code ... we believe that very talented employees of Square Inc. doing it better than we can.

Here is a picture

The following figure shows the heap time distributions recorded in the dalvikvm-heap log message. Red dots indicate that the user is introducing a fresh set of articles into the application to strengthen the amount of work and emphasize the application ...

DALVIKVM heap allocation http://snag.gy/FgsiN.jpg Figure 1: Nexus One heap allocation; OOM occurs at 80 MB +

Study to date

Compared to the Nexus S, Nexus 4, Wildfire, HTC Incredible, and many additional test devices, anonymous testing showed that memory management is enough for the DVM GC to β€œsupport” the heavy lifting operation, the application being completed. However, on high-end devices such as the Galaxy S II, III, IV, and HTC One, OOMs are common. In fact, given enough work, I would suggest that all of our devices will ultimately fail.

Question

There is a clear connection between screen density (our requested image sizes are based on ImageView size), the memory allocation of the process, and the number of images with a given size, which will lead to exceeding the application using heap restrictions. I'm going to start quantifying these relationships, but would like the SO community to highlight this issue and (a) agree or disagree that the relationship is worth it, and (b) provide literature indicating how to best compose the relationship .

It is important to note that if we destroy image quality, our OOM all disappear, but, alas, the UX is worse, and that is why we want it to be the most efficient use of the available heap.


Side note: here is the part of the code responsible for loading these images into the laid out views;

 picassoInstance.load(entry.getKey()) .resize(imageView.getMeasuredWidth(), imageView.getMeasuredHeight()) .centerCrop() .into(imageView); 

Image Quality Deprivation mentioned above simply divides imageView.getMeasured... by a number like "4".

+6
source share
2 answers

First you need to manage the memory allocation, this is a big problem in android, since bitmaps take up a lot of memories, because this memory allocation can be reduced by following the steps below.

  • put all those images that are huge in size in the data folder, instead of putting them in a drawable folder. because recoverable resources take up memory to cache. If you download from the resource folder, the image will not be cached. and there will be less memory.

    • Lrucache research, which is used for efficient memory management.
    • put resources in tiny formats for this TinyPNG check
    • If your images are too large in resolution, try using SVG files for images and upload an SVG file instead of an image. check out SVG FOR ANDROID

In the end, I'm not very good at English, I hope this can help you.

+2
source

This post is a bit old, but I have also had this problem lately. Maybe this will help someone else. A general overview of this massive topic / What helped me.

-Make sure you are using Singleton Instance of Picasso

-Use fit ()

- For large images or many images or when used in a FragmentPager / StatePager, you should probably use skipmemorycache () and / or a largeHeap declaration

Read the topic for more tips. At the time this question was submitted, no one posted this issue on picassos github.

https://github.com/square/picasso/issues/305

Good luck. Hope this helps and happy coding.

0
source

Source: https://habr.com/ru/post/957047/


All Articles