TreeView component by LinearLayout - out of memory error - android

I make a treeview component by LinearLayout base. But when inner items count is very large android close it by out of memory error.
I used only 3 bitmap by 3kb size in each items.
How can I reduce memory using of my component (LinearLayout) I know ListView has scrollingCache but I use LinearLayout ?
Can anyone help?
Thank you.

The ListView use a system that initialize only the row(view) that are visible to the user. When a row in not visible anymore it doesn't get destroyed, but it is used to inflate the layout of the next view that will be visible. With this system you have to save in memory only for the object that are visible, even if there is 1000 of item in your list.
Here you can find a more accurate description of the recycle system
I think you should also take a look at this video. It's a lesson on ListView from Google I/O. I find out it is very useful to understand how ListView works .
In your case, if you cannot use ListView, i think you should implements manually this system. This is pretty hard so the best advice i can give you is to try to create a custom ListView that fit your needs and use it.
PS:The method recycle() is a bit different, it is used to delete any reference to the Bitmap on which you are using the method. This way, the next time the GC run, it will be able to delete from memory the Bitmap

Related

Android ScrollView VS ListView show many dynamic items

There is object array received from server
there are two ways to show items :
Adapt Objects To ListView
Use Scroll View
So If I use scroll View and add items programmatically, There are some Questions:
Does Adding Items Programmatically Cause Out Of Memory ?
Does It Need To Remove Items from memory ? or it will remove automatic after onDestroy Called or Items moved out of screen?
Regards
If the data set is large, a ListView won't stop OOMs by itself, you will have to avoid loading the entire data set in memory by using a CusorAdapter backed by some non-memory data store (like a sql database).
A ListView will reduce the amount of memory that the Views use to represent the data to the user, since it will only initialise Views that the user can see (and recycle Views if possible when the user scrolls).
Since only the Views visible are loaded with a ListView, performance should* also be better since the layout process will be quicker.
In most cases, a ListView would be better for this type of thing.
(*) If the adapter's getView or bindView e.t.c. are complex, or each row in the list has its own view type, the scrolling performance could actually be worse.
Listview is the Best case for you. and I will prefer to use volly library of android . It is easy to use and very handy and helpfull.

how to prevent recycling of viewpager in listview item

I have a listview with viewpager(loads images asynchronously) in each list item. Since listview recycles images on scroll viewpager instantiates again as item goes off the screen. I have also considered using LinearLayout with adapter to populate instead of listview to prevent views recycling, but im not sure it could hold large data lists. any help??
You are clear, and what is more clear is that your architecture is terribly wrong…
The reason why the views get recycled is not because Android wants you to do extra work, it's because on a Mobile device, memory is LIMITED. If something is not visible, it should go away (most of the time).
You can increase the ViewPager offset a little bit more if you want something to stay for longer but the truth is that your image loading library should cache the images to the point where the next time you recreate them, they are instantly available, if there's memory to keep the cache alive or they should be simply re-downloaded. Welcome to Mobile Development.
Take a look at Picasso for example (on how to load images and cache them the right way).

Recycle views in scrollview in android

I have a scrollview in which I have inflated the view dynamically (same as facebook wall of android app, there is an imageview and textview in it.).
All the images are dynamic. If I load too much data in it then I get outofmemory exception. I know that listview will solve my problem but I cannot use the listview.
So, is there any way to optimize i.e recycle the views when they are not visible?
By your comments, I believe you still can use a list view:
A listview can have many different kinds of view. For more information on this, read Android ListView with different layouts for each row.
This way, views will be recycled and hopefully your memory usage will be smaller.
Anyway, I would probably investigate your code as I guess that for running out of memory, you probably are leaking objects or doing something way too many times than needed.
you can not handle Errors in normal circumstance but you can use following techneques to remove thias errror
1-Please clear your Arraylist before item add .
2-Use garbage collection techniques
System.gc() or finalize method use for Memory reclaim from object.

Keep the images downloaded in the listview during scrolling

I'm developing my first app and have been reading a LOT here.
I've been trying to find a solution for the following issue for over a week with no luck.
I have an Adapter that extends ArrayAdapter to show image and 3 lines of text in each row.
Inside the getView I assign relevant information for the TextViews and use ImageLoader class to download image and assign it to the ImageView.
Everything works great! I have 4.5 rows visible on my screen (out of total of 20). When I scroll down for the first time the images continue to download and be assigned in right order to the list.
BUT when I scroll back the list looses all the images and start redrawing them again (0.5-1 sec per image) in correct order. From what I've been reading it's the standard list performance but I want to change it.
I want that, once the image was downloaded, it will be "sticked" to the list for the whole session of the current window. Just like in Contacts list or in the Market. It is only 20 images (6-9kb each).
Hope I managed to explain myself.
you need to cache each image after download it, and each time the adapter need it check if its already downloaded get it from cache (disk or memory) otherwise download it.
at first i recommended you to read this tutorial from android dev site http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
or use an external lib like this one https://github.com/koush/UrlImageViewHelper
The problem here is that the ArrayAdapter is reusing the list view rows as you scroll, so as you scroll down, the top row will be reused and inserted at the bottom of the list (for performance reasons).
Your best bet here is to try to cache the images locally on your device to avoid calling the ImageLoader every time.
One pretty good library that solves this problem is ignition. It's open source and available here: https://github.com/kaeppler/ignition
Take a look at RemoteImageView for a good example of the caching mechanism.

Android View Scrolling Performance Troubleshooting

I'm currently experiencing some scrolling issues in a few of my views. The performance on the Galaxy Tab (7 inch) is rather poor. Rather than trying to explain my view hierarchy here, I was wondering if anyone had any general guidance on how to troubleshoot scroll performance issues on Android. I'm interested in any approaches or gotchas that I should be aware of when building views, and how to find the trouble spots that's causing the laggy scrolling
I these few suggestions would help you,
1- Use an efficient ListAdapter (taking advantage of the ConvertView in getView() method) as such:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
2- If you are loading images over the net, thread your loader and set images to views asynchronously.
3- And if you're rendering images in your list items, keep a cache in the form of an HashMap; i.e. HashMap where integer would be the position and Drawable would be the materialized images. Use the Drawables availabe in the Cache in your getView() method and only load images if the Drawable is null when you ask for a particular index/position.
As you may already know, list items are rendered (getView() is called) every time the the item at the index is scrolled to the visible screen; so caching images and using the convertView (the view already been created once) would make your adapter and list efficient.
You could even put some system.outs in your getView() method to see the difference.
I hope these help
Best
-serkan

Categories

Resources