1
java.lang.OutOfMemoryError

at android.view.ViewGroup.addInArray(ViewGroup.java:3576)

at android.view.ViewGroup.addViewInner(ViewGroup.java:3531)

at android.view.ViewGroup.addView(ViewGroup.java:3380)

In my Application I show the user all the phone contacts and ask the user to select few or all the contacts to be used in our application. I show the user all the contacts at once. No lazy loading or something like this. It working fine if i show them up to 1000 contacts but if there are more than 5000 contacts in the phone and if I try to show them in one go. I am getting OutOfMemoryError. How to solve this issue.

W00di
  • 954
  • 12
  • 21

2 Answers2

0

Implement a ListAdapter and view it with a ListView.

The ListView will recycle the memory when it needs to.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
andy256
  • 2,821
  • 2
  • 13
  • 19
  • would not it show again OutOfMemoryError if i try to show 5000 contacts in list view in one go?? – W00di Sep 02 '13 at 06:50
  • We don't know because we don't have enough info. But it is the first step. You may have to do more, such as only keep the selected items in memory. – andy256 Sep 02 '13 at 06:54
0

Go through ListView Tutorial and implement a ListView using ViewHolder model to recycle the memory whenever it need. It is expected to give the OutOfMemoryError if you add all the element at once . Where as in listView you can see all the contact by scrolling up and down, only the few content based on size of device will be allocated the memory at at a time

Shakeeb Ayaz
  • 6,200
  • 6
  • 45
  • 64
  • thanks for help...although i am able to show 3000 contacts at once but for 4000 contacts i am getting OutOfMemoryError. – W00di Sep 03 '13 at 06:23
  • it is expected to happen. Now ur device is showing 3000 contact but if u run on low-end device then it will load not more then 500 contact so better u Implement ListView. Otherwise u'll face several device specific issue. – Shakeeb Ayaz Sep 03 '13 at 06:32
  • i am implementing list view with a low end device and its showing 3000 contacts without any problem but its crashing with OutOfMemoryError for 4000 contacts.. – W00di Sep 03 '13 at 06:58
  • @sabyaHave u implemented using viewHolder model – Shakeeb Ayaz Sep 03 '13 at 07:06
  • no, i am implementing simply listview with baseadapter.. here iz the code of [baseadpater](http://pastie.org/8293575) and [my listview dialog](http://pastie.org/8293579) – W00di Sep 03 '13 at 07:17
  • @sabyaI explicitly said in my ans to implement ViewHolder model in ur code ,which u re not doing(i checked ur code).so its my last suggestion is to Implement paging(ViewHolder) to load data in list view form contact provider – Shakeeb Ayaz Sep 03 '13 at 07:22
  • check out this for ViewHolder http://stackoverflow.com/questions/4145602/how-to-implement-a-view-holder http://stackoverflow.com/questions/4145602/how-to-implement-a-view-holder – Shakeeb Ayaz Sep 03 '13 at 07:27