Android Endless List Memory Management
Solution 1:
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).
Solution 2:
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).
Also check this article Performance Tips for Android’s ListView
Solution 3:
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
publicclassMemoryActivityextendsActivity {
privateList<TestObject> _testObjects = newArrayList<TestObject>();
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_memory);
coolGuysDoNotLookBackAtExplosion();
starCountdown();
}
privatevoidstarCountdown() {
newCountDownTimer(300000, 500) {
publicvoidonTick(long millisUntilFinished) {
TextView tv_watcher = (TextView) findViewById(R.id.tv_watcher);
tv_watcher.setText(getMemoryUsage());
}
publicvoidonFinish() {
starCountdown();
}
}.start();
}
privateStringgetMemoryUsage() {
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;
}
privatevoidcoolGuysDoNotLookBackAtExplosion(){
newThread(newRunnable() {
@Overridepublicvoidrun() {
_testObjects = newArrayList<TestObject>();
while (true) {
_testObjects.add(newTestObject());
}
}
}).start();
}
}
//TestObject
publicclassTestObject {
privateStringsampleText="Lorem Ipsum is simply dummy text of the printing and typesetting industry";
}
Solution 4:
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.
Solution 5:
You should use paging
and Load More
button as a footer of ListView
. For example:
url = http://your_full_url.php?page=1
let say you have 100 records in each page then very first time get all these 100 records of page 1
, display those on ListView
and cache
them. Now scroll down your ListView
and click on Load more button (Load More button should be set as footer of the ListView
).
When you click on Load More you will get next 100 records by calling
url = http://your_full_url.php?page=2
and so on for
url = http://your_full_url.php?page=3
,
url = http://your_full_url.php?page=4
etc...
each time you will cache those records so that in case of connection loss you could show records available in cache.
Post a Comment for "Android Endless List Memory Management"