Android Endless list memory management

后端 未结 9 2078
温柔的废话
温柔的废话 2021-02-05 07:42

I\'m implementing an endless listview by loading more items to the arraylist in the onScrollStateChanged(...) method. If I\'m implementing this scheme for fetching more than 1 m

相关标签:
9条回答
  • 2021-02-05 08:26

    I think you should just keep the current entries and the one just before or after them(maybe 100),put this data to a cache.

    When you scroll your listview,fetch more entries and update the cache as before(do not get 1 million at one time).

    0 讨论(0)
  • 2021-02-05 08:26

    This question has nothing to do with the Adapter “capacity”. Instead, it is related to the amount of memory allocated by your app.

    It has a reserved heap in order to allocate objects, if you pass this limit you will get an Out of Memory Exception

    Here a little test, it could give you an idea about the amount of data that you could allocate. But be aware that in this example the object contains just an String, if it were a gorgeous Bitmap the amount of objects to allocate would be much much much less.

    //MemoryActivity

    public class MemoryActivity extends Activity {
    
        private List<TestObject> _testObjects = new ArrayList<TestObject>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test_memory);
            coolGuysDoNotLookBackAtExplosion();
            starCountdown();
        }
    
        private void starCountdown() {
            new CountDownTimer(300000, 500) {
    
                public void onTick(long millisUntilFinished) {
                    TextView tv_watcher = (TextView) findViewById(R.id.tv_watcher);
                    tv_watcher.setText(getMemoryUsage());
                }
    
                public void onFinish() {
                    starCountdown();
                }
    
            }.start();
        }
    
        private String getMemoryUsage() {
            String heapSize = String.format("%.3f", (float) (Runtime.getRuntime().totalMemory() / 1024.00 / 1024.00));
            String freeMemory = String.format("%.3f", (float) (Runtime.getRuntime().freeMemory() / 1024.00 / 1024.00));
    
            String allocatedMemory = String
                    .format("%.3f", (float) ((Runtime.getRuntime()
                            .totalMemory() - Runtime.getRuntime()
                            .freeMemory()) / 1024.00 / 1024.00));
            String heapSizeLimit = String.format("%.3f", (float) (Runtime.getRuntime().maxMemory() / 1024.00 / 1024.00));
    
            String nObjects = "Objects Allocated: " + _testObjects.size();
    
            return "Current Heap Size: "    + heapSize
                    + "\n Free memory: "
                    + freeMemory
                    + "\n Allocated Memory: "
                    + allocatedMemory
                    + "\n Heap Size Limit:  "
                    + heapSizeLimit
                    + "\n" + nObjects;
        }
    
        private void coolGuysDoNotLookBackAtExplosion(){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    _testObjects = new ArrayList<TestObject>();
                    while (true) {
                        _testObjects.add(new TestObject());
                    }
                }
            }).start();
        }
    }
    

    //TestObject

    public class TestObject {
        private String sampleText = "Lorem Ipsum is simply dummy text of the printing and typesetting industry";
    }
    
    0 讨论(0)
  • 2021-02-05 08:26

    I couldn't find an exact number mentioned in the docs. However, the return types of all Adapter#getCount() (look at the subclasses) are ints.

    Therefore, we can strongly suspect you can add up to Integer.MAX_VALUE items into an adapter, which is 231-1 (over 2 billions). Adapters use Lists and Maps to store the data internally, which have the same limit.

    So you should not be worried about the limitations of the adapter rather than using too much memory. I suggest you to load 10-100 elements into the adapter and simply add more items as soon as the user reaches the bottom of the listview.

    0 讨论(0)
提交回复
热议问题