Android Endless list memory management

2020-05-25 06:06发布

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 million entries, I will have a million objects added to the arraylist, which is memory intensive. What schemes can I use for efficient memory management ?

PS: The question is about the number of items that can be put into the adapter. Edit:

More details:

The source of the data is Internet. I have to fetch the data from the Internet and put it into the listview adapter.

9条回答
甜甜的少女心
2楼-- · 2020-05-25 06:31

If your ListView contains only text items, there is not much you need to do. However, if you are loading more memory intense things, like drawables (for example, you have a picture on the right side of your view), then you should do some recycling, for best result. You might receive an OutOfMemoryException very quickly on a weaker device. I could go OOM even on a Nexus 4. Just try to scroll very quickly, up and down, up and down, and repeat until force close.

Take a look at RecyclerListener, it is very easy to implement.

查看更多
迷人小祖宗
3楼-- · 2020-05-25 06:32

In Android the ListView is virtualized. Practically that means that there's no actual limit for number of elements inside it. You can put millions of rows inside the list, it'll only allocate memory for the currently visible ones (or a few more tops).

Source

Also check this article Performance Tips for Android’s ListView

查看更多
在下西门庆
4楼-- · 2020-05-25 06:32

Tianwei's approach is the way to go.

If the ListView is lazy loaded and since the ListView itself is recycling views it's best to keep only the visible list entries in memory. You basically do the same in the adapter the ListView does for the Views.

If you'd keep all data in memory what would be the point of lazy loading the ListView anyway? Just load all data and skip the lazy loading part... Of course with the lazy loading approach that does load only the visible data (and maybe some more) you'd have to implement lazy loading at the bottom and the top of the list to make this work.

Now since there's no information on the nature of the data (text, images) or the source (Internet, SQLite Db, text file...) I can't give you code (samples) how to implement this. If you elaborate on the data I can answer the question more accurately.

查看更多
神经病院院长
5楼-- · 2020-05-25 06:33

I guess sqlite Database and streaming parser(GSON).

查看更多
聊天终结者
6楼-- · 2020-05-25 06:37

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).

查看更多
Evening l夕情丶
7楼-- · 2020-05-25 06:38

If you need to keep in memory 1M objects, and assuming the object data is small, than theis is just a few MB of memory, and should be fine to just keep in memory. From the question I understand that you will read more items when users scrolls forward, so in practice you will not have 1M rows - users will need to scroll for a long time to get to 1M. As long as you use the ListView properly, you can have the adapter data grow to be 1M+ rows in memory without any issue

查看更多
登录 后发表回答