Hi, Habr! In this article, focused on beginners, I would like to give some tips on how to optimize the use of device memory by the application, so as not to receive OutOfMemory all the time, and also consider using vector images in the current actual 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 raster images.
Of course, large images with many small details should not be translated into a vector - it will take as much space as a raster, if not more. However, small images, such as icons and other parts of the user interface should be converted to a vector in order to save memory. Yes, and work with
vector images are often much more convenient. And now, the most important thing is how to do it?
- Open Android Studio.
- We right-click on the drawable folder or its contents> New> Vector Asset

- Specify the path to your svg-file. If your image has an irregular shape, then I advise you to put a tick next to the Override parameter - otherwise the picture will fit the standard dimensions, which can distort its proportions.
')

- Next> Finish
- Done!
To convert from raster to vector, I can advise a great free application
Inkscape . A little about working with him:
- Open Inkscape.
- Drag any raster into it. In the window that opens, select the import settings and click OK.
- In the upper toolbar, after selecting our image, select Contour> Vectorize raster (Shift + Alt + B).
- Now the most important thing. In the new window, check the box next to “Remove background” and select the following: will our image be in color and how many scans should be made. 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 needed 30 scans to get a divine look, due to the large number of colors in the image. This is quite a lot, it is better to do no more than ten and choose pictures simpler.

- We 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 to 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 image to raster and vector. We look the data.
Raster

Vector

The difference is almost doubled. I think this is quite convincing.
2. Increase heap size
To do this, go to the manifest of your project (app> manifests> AndroidManifest.xml) and in the application column add the line:
android:largeHeap="true"
In fact, increasing the heap is not a solution to OutOfMemory, but pushing it to a far shelf. Instead of optimizing the application's use of device memory, we give it more space. Do not forget that each device has its own memory capacity, both main and additional, allocated for the application.
3. Avoid memory leaks.
Any application in its work uses a variety of objects that, naturally, occupy a certain place in the memory. Ideally, the garbage collector should remove unused objects from it, but sometimes so-called “memory leaks” appear that cause serious problems in the operation of the application. There are various causes of memory leaks, which are described in detail
here .
On my own, I would like to recommend the
WeakHandler library, developed by Badoo, and designed to fix memory leaks associated with the incorrect use of android.os.Handler. To use this library, add to your gradle-file (Gradle Scripts> build.gradle (Module: app)) in the dependencies column the line:
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 frame-by-frame animations.
Frame-by-frame animation in Android Studio is a handy thing, but far from the most economical. When using a large number of images in it, you will certainly get OutOfMemory.
But, if you really need it, better use the gif-image with the
Android Gif Drawable library. This library simplifies c-gif work and also consumes much less memory than Android Studio frame-by-frame animations. To use this library, add to your gradle-file (Gradle Scripts> build.gradle (Module: app)) in the dependencies column the line:
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.16'
and in your second gradle file (Gradle Scripts> build.gradle (Module: “the name of your application”)) in the buildscript and allproject columns line:
mavenCentral()
and in the java file:
GifDrawable gifFromResource = new GifDrawable( getResources(), R.drawable._ ); gifFromResource.start();
To disable gif instead of start (), we write stop (). Also do not forget to compress gif-ki, it will save even more space.
I hope that my article was useful to you. Thank.