How to empty or clear heap memory dynamically

unkown picture unkown · Apr 17, 2012 · Viewed 8.6k times · Source

My project is totally bases on images. I am using a gallery and a coverflow in my project and due to less heap size the app is forcefully close.

So every time I load my app I want to clear heap size, so I can reuse it.
I have used System.gc() & recycle () method in gallery and coverflow, but my gallery and coverflow, are inter-related so the object is always referenced, so System.gc() and recycle() are not having effect and my gallery and coverflow still crashes at a random point of time while using it.

For some reason I can't show my code here and even if, it is very big so I can't manage to show here.

Please help me out & thanks for reading this question.

Answer

Kumar Bibek picture Kumar Bibek · Apr 17, 2012

Well, You don't have any real control over forcing the GC to run. It runs on it's own, whenever it thinks it's time.

When does System.gc() do anything

So, the only option that you have is to judiciously use images and bitmaps.

Hint => Use soft references. http://developer.android.com/reference/java/lang/ref/SoftReference.html