How to Detect Lowmemory in android?

前端 未结 7 1660
-上瘾入骨i
-上瘾入骨i 2021-02-13 14:34

My application has a lot of images and sometimes it crashes due to low memory. I wrote this function that I found on the developer site.

public void onLowM         


        
相关标签:
7条回答
  • 2021-02-13 15:12

    Check Memory Analysis for Android Applications .

    0 讨论(0)
  • 2021-02-13 15:12

    From API level 14, there's method called onTrimMemory(int) that fit your need.

    Here's the doc:

    http://developer.android.com/reference/android/content/ComponentCallbacks2.html

    You're able to be notified when the system is about to recollect memory and also how critical the current situation is.

    0 讨论(0)
  • 2021-02-13 15:20

    onLowMemory() is not a big help because it gets called only at times where the overall system is running out of memory and not in cases where your app is running out of its available heap space.

    So my answer is that you should not depend on onLowMemory() being invoked. And IMHO there is no callback method for your problem.

    You can simply poll the available heap space and check whether you are running out of memory or not.

    Runtime.getRuntime().maxMemory();
    Runtime.getRuntime().totalMemory();
    Runtime.getRuntime().freeMemory();
    
    0 讨论(0)
  • 2021-02-13 15:20

    My application has a lot of images and sometimes it crashes due to low memory

    That part is not clear, if your application is downloading any image from internet or end user picking image from gallery and adding it? In this case

    • you can warn the user that in order to add new images they must move back some already picked images
    • Or better you can remove the image from memory yourself using any algorithm(how about least recently used/viewed?)

      Another case is your application itself is using too much memory because of unneeded references and leaks..This case you never warn the user that there is no more memory..It is simply your responsibility to effectively use memory...This case normally arises mainly when

    • You are leaking a view/layout which are using many images, in this case you must find out the leak and resolve it..If you are using eclipse read about MAT
    • Rarely when the images are too big in size..This is a designers error..You can get good look and feel with images in the size of few kilo bytes...

    In any case, it is unwise to alert user of low memory..A programmer should always program thinking you can make the app better by using less memory..

    This thread will be very useful to know about your memory footprint inforamation..

    And about usage of MAT tool...

    0 讨论(0)
  • 2021-02-13 15:26

    Along with onLowMemory() and onTrimMemory(), which monitor system memory, here's a way to monitor app memory. It uses Zsolt Safrany's answer at it's core.

    private final float CHECK_MEMORY_FREQ_SECONDS = 3.0f;
    private final float LOW_MEMORY_THRESHOLD_PERCENT = 5.0f; // Available %
    private Handler memoryHandler_;
    
    public void onCreate( Bundle savedInstanceState ) {
    
    // Do Stuff...
    
     // Start Monitoring App Memory Availablity
        memoryHandler_ = new Handler();
        checkAppMemory();
    }
    
    public void checkAppMemory(){
        // Get app memory info
        long available = Runtime.getRuntime().maxMemory();
        long used = Runtime.getRuntime().totalMemory();
    
        // Check for & and handle low memory state
        float percentAvailable = 100f * (1f - ((float) used / available ));
        if( percentAvailable <= LOW_MEMORY_THRESHOLD_PERCENT )
            handleLowMemory();
    
        // Repeat after a delay
        memoryHandler_.postDelayed( new Runnable(){ public void run() {
            checkAppMemory();
        }}, (int)(CHECK_MEMORY_FREQ_SECONDS * 1000) );
    }
    
    public void handleLowMemory(){ 
        // Free Memory Here
    }
    
    0 讨论(0)
  • 2021-02-13 15:26

    As explained at android developers is hard to determine how much memory we have available for our app since: The exact heap size limit varies between devices based on how much RAM the device has available overall. So is a better approach to ask if we are running in a lowMemory environment using ActivityManager.MemoryInfo before doing a memory heavy operation like downloading an image or trying to load an image into memory, by doing this you will prevent OutOfMemoryExceptions

    public void doSomethingMemoryIntensive() {
    
        // Before doing something that requires a lot of memory,
        // check to see whether the device is in a low memory state.
        ActivityManager.MemoryInfo memoryInfo = getAvailableMemory();
    
        if (!memoryInfo.lowMemory) {
           // Do memory intensive work ...
        }
    }
    // Get a MemoryInfo object for the device's current memory status.
    private ActivityManager.MemoryInfo getAvailableMemory() {
        ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(memoryInfo);
        return memoryInfo;
    }
    
    0 讨论(0)
提交回复
热议问题