Glide Memory Processing Optimization

Source: Internet
Author: User
First, the preface

Glide is Google's official recommendation of a picture loading library, the use is very simple and convenient, Glide it to help us accomplish a lot of important, but universal work

Can, for example: Image loading compression, display, load picture memory management and so on.

Glide is not familiar with the friend, you can refer to a good article to help you get started Glide

However, when using Glide, there are a few tricks that can make your memory more optimized to avoid possible OOM. For example: Although Glide will be based on the controls that are loaded

Size, optimize the loading of the picture size, but if the load is a large Zhang Quan, will still occupy a very large memory space operation.

A specific Bitmap how much memory space to occupy, you can refer to the "Bitmap than you want to cost more memory | Diaoda OOM "

Some of this article is recommended from the Android TV App, while Android TV's many smart TVs and smart boxes actually have very bad hardware conditions, while the Android T

V's App, in order to beautify, will use most of the pictures, so in the picture use, OOM problem will be magnified, and the following described some of the optimization side

Case, in the Android phone hardware conditions are very good environment, do not use the impact is not small. second, start to optimize 2.1 Configuration Good trimmemory and Lowmemory

Glide helps us do most of the memory management stuff, and it actually supports doing better.

In the case of an App, a callback to some ontrimmemory () or onlowmemory () in the event of a low system memory environment is used to remind

, the current device's memory environment has changed, you'd better adjust your memory usage strategy to avoid being cleaned up or OOM by the system.

About Ontrimmemroy () related content, do not understand the first reference to "Android development, skip memory management"

And Glide also provides us with a similar approach to the interface, developers only need to call, it will be internally with different memory conditions, help us to the cached picture optimization.

Here, you mainly use the Glide trimmemory () and Cleanmemroy () methods, which are used to crop the memory space of the Glide cache, one to clear

Glide the cached memory space.

Before using Ontrimmemory (), the COMPONENTCALLBACKS2 interface is generally implemented, and then in application, through the Registercomponentcallback

S () method to register. Of course, if you bother, you can also directly in the application, rewrite the corresponding method.

Knowing these, we can configure when to call Glide's corresponding method according to our needs, the configuration I recommend: When Lowmemory, call Glide.cleanmemroy () to clean out all memory caches. When the App is replaced in the background, call Glide.cleanmemroy () to clean out all the memory caches. In other Ontrimmemroy () callbacks, call the Glide.trimmemory () method directly to the Glide to process the memory.

Then the corresponding code, as follows:

Now that we know the two ways to call Glide, we still need to know what the inside of it is doing for us. First look at the Glide corresponding source code.

Within these methods of Glide, you can see that they are going to manipulate both memorycache and Bitmappool objects, in fact they are two interfaces, where if you do something special, the operation

are Glide to their default implementations, Lruresourcecache and Lrubitmappool. As can be seen from the name, they all follow the LRU algorithm.

In the case of Glide, Memory cache is Glide used to cache picture resources in memory so that they can be used immediately when needed, without the need to perform a magnetic

Disk I/O operations, while Bitmatpool is Glide maintains a picture reuse pool, Lrubitmappool uses the LRU algorithm to retain the most recently used size BITMA

P, this is not the focus of this article, we can understand.

In fact, in Lruresourcecache and Lrubitmappool, the operation of Clearmemory () and Trimmemory () is similar, where lrubitmappool

Cases.

In Lrubitmappool, the call to Clearmemory () or TrimToSize () is based on the callback method and the parameters, which are ultimately called Trimtosiz

E () method. It is used to crop the current number of cached resources.

As you can see, depending on the target size of the crop, you will reclaim the extra Bitmap to the appropriate target size to clean up the memory. 2.2 Configuration Glidemodule

Glidemodule is a configuration interface provided by Glide that is invoked at the first use of Glide for some initial configuration of Glide.

For specific Glidemodule, please refer to the official documentation:

Https://github.com/bumptech/glide/wiki/Configuration

Glidemodule is an interface that needs to implement its corresponding method.

Here we just need to use the Applyoptions () method, which is used to append some of the configuration we need based on the Glide default configuration.

In this case, we can set the current device according to the memory condition, and use Activitymanager to get the current device memory condition, if it is in Lo

When Wmemory, set the Decodeformat of the picture to rgb_565, rgb_565 and the default argb_8888 ratio, each pixel will be 2 byte less, which

Like, equal to a picture of the same, loaded into memory will be less than half of the memory occupied (argb_8888 4 byte per pixel).

2.3 Avoid using rounded corners of the ImageView

In the actual project, often use some with rounded corners of the picture, or directly is a circular picture. Circular pictures, mostly used for the display of some user's avatar

Fruit.

And Android, there are a lot of similar xxximageview open source control, used to operate Bitmap to achieve a rounded image of the effect, such as Github on fire

of Roundedimageview.

Most of them are the principle of receiving the Bitmap you pass, and then outputting a new Bitmap that is larger than the original Bitmap, and on this basis, the rounded corners of a

Some processing, which causes, in fact, in memory, more than a Bitmap, a picture of the memory occupied by doubled.

So now that you have chosen to use Glide, it is recommended that you use Glide-transformations, the Open Source Library, glide-transformations use Glide Bitmaptra

Nsfrom () interface to implement some transformation operations on the loaded Bitmap.

Glide-transformations's Github address is as follows:

Https://github.com/wasabeef/glide-transformations

Glide-transformations provides a series of transform operations on loaded pictures, from shape transformation to color transformation, all supported, basically satisfying most development needs, and

It will reuse the Glide Bitmappool, to achieve the purpose of saving memory.

The use of specific glide-transformations, you can view the document on the Github, the following is a picture of its effect.

2.4 Crop your picture according to the memory condition

Some of the previous optimization points are recommended common practices, basically using the approach described earlier, the picture caused by the OOM should be significantly reduced.

Next, let's introduce a solution to the problem of optimizing memory when loading a full-screen image on an Android TV.

The first thing to be clear, the domestic Android TV hardware environment is very bad, Sambei smart boxes are sold everywhere, after all, is running the Android system, you think

You're using a 299 Android phone and you don't expect anything from it. But the Android TV is made for television, so in most cases it

Is the need to support 1920 * 1280 screen size, causing it to load a large image of the Zhang Quan, memory consumption is not to be looked at, if in the memory environment bad feeling

condition, it may be directly OOM collapse.

So, for this extreme situation, I think of a way, according to the current memory environment, proportionally reduce the need to display the full screen picture, so loaded into memory diagram

Film, is proportional to the reduction.

Here you need to use the Drawablerequestbuilder override () Api, which can accept a width and height, to reassign the loading image

Size.

Now that Glide has provided a standard API, we also need to get the height of the currently running device.

It is recommended that you use the Getrealsize () method to get the width of the screen, which can actually get the size of the current screen. Other APIs on some smart TVs and boxes, take

The size will be small, because there is no calculation StatusBar or navigationbar height, these are the experience of experience.

At the same time, we also need to use the ComponentCallbacks2 interface, which has been introduced before, will not repeat.

In which, the value of the level of the trim is recorded, the current memory levels are reflected, and when used, a getbitmapsize () is cropped to match the current memory environment.

Size.

In the example, only the Trim_menory_running_low is processed, which is scaled to 0.8f times according to the screen size. If you want to do more, you can put it

It is also a few level plus, adjust different zoom multiples.

Two output, look at the difference, the same Zhang Quan picture, do not scale and zoom 0.8f difference.

I/cxmydev:bgimage bytecount:8294400
i/cxmydev:bgimage bytecount:5308416
1 2

As can be seen, the purpose of optimization is still achieved. Can save about 3MB of memory space, and the picture is not blurred to see the point. Third, summary

Optimization is not the end, today, first chat here, after the thought of the supplementary. If you have any better suggestions, you can discuss them together at the end of the text.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.