Coder Social home page Coder Social logo

oomexample's Introduction

OOMExaple

基于recyclerview和Bitmap加载大量图片时会消耗内存,如果不及时清理的话,会产生OOM的错误。 Bitmap类有一个recycle()方法可以回收内存,但是回收后的子项滑动到眼前,有会因为图像资源被回收而出现错误。
所以在加载到眼前前,又要重新给Imageview加载一个有资源的bitmap.
需要注意的是:
适配器中的onBindViewHolder在让子项出现在眼前时均会调用,即使之前已经出现在眼前过,但是滑动回来又会调用。
下面的代码在MainActivity中:目的是使即将进入视野的子项重新加载,退出后的子项回收bitmap

 //上拉
                if (firstOld < firstNew) {
                    firstOld++;
                    itemAdapter.release(firstNew - 2);
                    itemAdapter.resetItem(firstNew + visibleCount);
                }
                //下拉
                if (firstOld > firstNew) {
                    firstOld--;
                    itemAdapter.resetItem(firstNew - 1);
                    itemAdapter.release(firstNew + visibleCount + 1);
                }

效果如下,滑动时内存占有未变:

oomexample's People

Contributors

hetingwei avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.