Android Endless list memory management

后端 未结 9 2079
温柔的废话
温柔的废话 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

    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 _testObjects = new ArrayList();
    
        @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();
                    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";
    }
    

提交回复
热议问题