OutOfMemory and the use of vector images in Android Studio

Hello, Habr! In this article, aimed at beginners, I would like to give some tips on optimizing the use of the device’s memory by the application, so as not to constantly get OutOfMemory, and also consider the use of vector images in the current version of Android Studio (3.4), since most Russian-language resources on this The topic (the last article on Habré about vector images dates back to 2015) is outdated, which often misleads novice developers. So let's get started.

1. Use vector images instead of bitmaps


Of course, large images with many small details should not be converted into a vector - this will take up as much space as a raster, if not more. However, small images, such as icons and other details of the user interface should be converted to a vector in order to save memory. And working with vector images is often much more convenient. And now, the most important thing - how to do it?

  1. Open Android Studio.
  2. Right-click on the drawable folder or its contents> New> Vector Asset

  3. Specify the path to your svg file. If your image has an irregular shape, then I advise you to check the box next to the Override parameter - otherwise the image will be adjusted to standard sizes, which may distort its proportions.

  4. Next> Finish
  5. Done!

For converting from raster to vector, I can recommend an excellent free Inkscape application . A little about working with him:

  1. Open Inkscape.
  2. Drag any raster into it. In the window that opens, select the import options and click OK.
  3. In the upper toolbar, after selecting our image, select Path> Vectorize raster (Shift + Alt + B).
  4. Now the most important thing. In the new window, check the box next to “Remove background” and select the following: whether our image will be color, and how many scans to make. We must not forget that the size of our vector file directly depends on these two parameters.

    More scans - more colors, shades and details. My leprechaun, in order to acquire a divine appearance, because of the greater number of colors in the image, 30 scans were required. This is quite a lot, it is better to do no more than ten and choose pictures easier.

  5. Close the window, click on the raster and delete it with the Delete key, go to File> Document Properties (Shift + Ctrl + D), adjust the page size for the content.

Now we will carry out a small test proving the advantage of the vector in saving memory.
I created a new project with a single ImageView, to which I applied an animation that moves it from point A to point B, alternately changing the images to raster and vector. We look at the data.

Raster



Vector



The difference is almost two times. I think this is quite convincing.

2. Increase the heap size


To do this, go to the manifest of your project (app> manifests> AndroidManifest.xml) and add the line in the application column:

android:largeHeap="true"

In fact, increasing the heap is not a solution to the OutOfMemory problem, but rather pushing it to the far shelf. Instead of optimizing the application’s use of the device’s memory, we give it more space. Do not forget that each device has its own memory capacity, both primary and secondary, allocated for the application.

3. Avoid memory leaks


Any application in its work uses many objects, which, of course, occupy a certain place in memory. Ideally, the garbage collector should remove unused objects from it, but sometimes there are so-called “memory leaks” that cause serious problems in the application. There are various causes of memory leaks, which are described in detail here .

On my own, I would like to advise the WeakHandler library , developed by Badoo, and designed to eliminate memory leaks associated with the misuse of android.os.Handler. To use this library, add the following line to your dependencies column in your gradle file (Gradle Scripts> build.gradle (Module: app)):

compile 'com.badoo.mobile:android-weak-handler:1.1'

and in the java file:

private WeakHandler mHandler;
    protected void onCreate(Bundle savedInstanceState) {
        mHandler = new WeakHandler();
        ...
    }
    private void onClick(View view) {
        mHandler.postDelayed(new Runnable() {
            view.setVisibility(View.INVISIBLE);
        }, 5000);
    }

And do not forget to import WeakHandler itself if the studio did not do this automatically.

4. Avoid large time-lapse animations


Frame-by-frame animation in Android Studio is a convenient thing, but not the most economical one. If you use a large number of images in it, you will certainly get OutOfMemory.

But, if you really needed it, it is better to use the gif image along with the Android Gif Drawable library . This library simplifies work with gifs and also consumes much less memory than frame-by-frame animations of Android Studio. To use this library, add the following line to your dependencies column in your gradle file (Gradle Scripts> build.gradle (Module: app)):

implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.16'

and in your second gradle file (Gradle Scripts> build.gradle (Module: “your application name”)) in the buildscript and allproject columns the line:

mavenCentral()

and in the java file:

GifDrawable gifFromResource = new GifDrawable( getResources(), R.drawable.имя_файла );
gifFromResource.start();

To disable gif, instead of start (), write stop (). Also do not forget to compress gifs, this will save even more space.

I hope that my article was useful to you. Thanks.

Also popular now: