How to Detect Lowmemory in android?

前端 未结 7 1650
-上瘾入骨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条回答
  •  -上瘾入骨i
    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...

提交回复
热议问题