I'm maintaining a big app with a huge number of images. My main problem is the app crashes when I use it because it produces out memory error. I'm trying to use SoftReferences and WeakReferences, I've read about it, but I don't know if I have to use them in Android classes like GridView, BaseAdapter or ImageView.
My app have a map Activity with markers which has a Gallery on the top. When I click over a balloon a second activity appears with a gridview.
The first activity, when I move the map the activity reload all the marker and images in the gallery and add new markers and images. It has the following attributes:
ShotButton shotButton; Gallery gallery; List images; private ImageView mMyLocationImage;
Dialog spinnerDialog;
private GoogleMap map;
private HashMap<String, Marker> identified_marker = new HashMap<String, Marker>();
private HashMap<Marker, DavinciImage> identified_XXX_images = new HashMap<Marker, XXXImage>();
private GalleryAdapter gallery_adapter;
private AsyncTask<Poi, Void, Void> refresh_pois_async = null;
@Override
public void onCreate(Bundle savedInstanceState) {
----
new SoftReference<List<Object>>(XXXImages);
new WeakReference<HashMap<String, Marker>>(identified_marker);
new WeakReference<HashMap<Marker, XXXImage>>(identified_XXX_images);
}
My question is when I move the map, destroy or pause the activity I have to do any new thing with soft/weakreference classes to free memory? Do I have to add a new attibute with a soft/weakreference class, e.g. the GalleryAdapter has to be added in a soft/weakReference class or the system free this memory for me?
The second activity has a gridview with all the images of the user. I load all the data when I enter to the activity and load the images when the activity needs. I have the following in this activity.
ImageView mUserImage;
public String myApplicationUserId;
public GridAdapter imageAdapter = null;
public GridView gridview = null;
public ImageLoader_Profile mImageLoaderMedium;
---
gridview.setScrollingCacheEnabled(true);
gridview.setFriction(10);
---
new WeakReference<ImageLoader_Profile>(mImageLoaderMedium);
The ImageLoader_Profile manage the load of images.
public class ImageLoader_Profile{
MemoryCache memoryCache = new MemoryCache( );
private Map< ImageView, String> imageViews = Collections.synchronizedMap( new WeakHashMap< ImageView, String>( ));
public ImageLoader_Profile( Context context, Bitmap canvas, int deviceDensity) {
....
new SoftReference<MemoryCache>(memoryCache);
new SoftReference<Map<ImageView,String>>(imageViews);
}
Is it all I need to do or I have to do anything else with soft references in this case? I'm a little confuse.
you should not soft /weak reference cache anymore.
It is changed that garbage collection process since Android 2.3,soft weak reference cache system is ineffective .
now, you should use LruCache,this is good :)
how to use LruCache? try this