Search code examples
androiduniversal-image-loaderandroid-volleyvirtual-memory

Android clean Virtual Memory


My application loads alot of data from a webservice. The problem is, after alot of network requests, it crashes from out of memory.

I'm using the volley library for the network requests, the Universal ImageLoader library for the loading and caching of images.

How do I prevent it from crashing? Is it possible to clean the virtual memory?


Solution

  • You can't tell JVM (Dalvik in this case) to clean memory "right now", because garbage collector will start (according to JVM specification) when it thinks a good time to do so. What you can do though is this:

    1. Assign null to all variables that refer objects that you don't need any more.
    2. Pay special attention to static variables that refer to big Java objects.
    3. Call System.gc() explicitly from your code, although remember that that call doesn't mean that garbage collection will start immediately. Rather consider this as an advice to JVM to start garbage collection as soon as possible.