0

In my application i override the onLowmemory method. It gets called every time. And my mobile is stuck. How to release memory in this method.

I am using below code :

@Override
    public void onLowMemory() {
        // TODO Auto-generated method stub
        super.onLowMemory();
        System.out.println("*************************************************");
        System.out.println("********** on low memory ************************");
        System.out.println("*************************************************");
        System.gc();
    }

Only using system.gc ... thank you.

George Thomas
  • 4,566
  • 5
  • 30
  • 65
Siri
  • 701
  • 1
  • 6
  • 27
  • You should not use System.gc();, find what consume your memory(maybe large images) – Piotr Golinski Apr 24 '15 at 08:32
  • Have you read the [Javadoc for `onLowMemory()`](http://developer.android.com/reference/android/app/Application.html#onLowMemory%28%29)? *"You should implement this method to release any caches or other unnecessary resources you may be holding on to. The system will perform a garbage collection for you after returning from this method."* So, 1) calling `System.gc()` is useless, and 2) you should release any caches/resources residing in memory that you don't absolutely need to keep your app functioning (i.e. stuff that can be reloaded from disk/db/network etc). – MH. Apr 24 '15 at 08:35
  • This method is called when the overall system is running low on memory, and actively running processes should trim their memory usage. While the exact point at which this will be called is not defined, generally it will happen when all background process have been killed. That is, before reaching the point of killing processes hosting service and foreground UI that we would like to avoid killing. read official android document http://developer.android.com/reference/android/app/Application.html#onLowMemory%28%29 – Amol Desai Aug 20 '15 at 05:39

2 Answers2

1

You should never call System.gc() it creates performance issues. You have to free references to any heavy objects you are holding - Look at the footprint of your application. Take a heap dump and see it in MAT. Look at what objects are taking most memory and check if you can free them.

RocketRandom
  • 1,102
  • 7
  • 20
0

I don't know what's going on, but my application stopped killing lowMemoryKiller when I added this code:

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        Runtime.getRuntime().gc();
    }
Master
  • 690
  • 6
  • 18