Determining the available heap space for an Activity (preventing an OutOfMemoryException)

I read. How do I know the memory usage of my application in Android? and a bunch of other answers, but I can’t do it ..

I have an Activity that will load a file from external storage into memory and do some sorting / manipulation of / etc in memory. Before loading it, I want to guess whether it will do this, will result in an OutOfMemoryException and cause the Activity to fail (I understand that exact answers are impossible, the evaluation is better than nothing).

From the above answer, I came up with:

ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(ACTIVITY_SERVICE); MemoryInfo memoryInfo = new ActivityManager.MemoryInfo(); activityManager.getMemoryInfo(memoryInfo); int pid [] = {android.os.Process.myPid()}; android.os.Debug.MemoryInfo[] mi = activityManager.getProcessMemoryInfo(pid); // calculate total_bytes_used using mi... long available_bytes = activityManager.getMemoryClass()*1024*1024 - total_bytes_used; 

So the questions are:

1) Am I crazy?
2) how to sum the values ​​from the MemoryInfo object to evaluate the use of the activity / task heap? (The link above gives an overview of pss / private-dirty / shared-dirty, but not enough information to figure out how to do this.)
3) Does debugging always exist or only when debugging?
4) is there a more reasonable way?

Answers to the following questions: Two questions about the maximum heap sizes and available memory in android seem to imply that there is no better way than this:

I know that using less memory is good, and so am I. I am interested to know how to protect the code here. It seems strange to just wait until the exception finds out that you have lost your memory.

Thanks!

+7
source share
1 answer

you can refer to this link .. Complete request for the same problem you are facing. OOMRESEACH

0
source

All Articles