7

I've read How do I discover memory usage of my application in Android? and a bunch of other answers, but can't quite nail this down...

I have an Activity that will load a file from external storage into memory and do some parsing/manipulation/etc in-memory. Before I load it I want to guess whether or not doing so will cause an OutOfMemoryException and crash the Activity (I understand that exact answers aren't possible, an estimate is better than nothing.)

From the above-linked 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:

1) am I crazy?
2) how to total the values from the MemoryInfo object to estimate the heap usage of the activity/task? (The above link gives an overview of pss/private-dirty/shared-dirty, but not enough info to guess how to do the total.)
3) does Debug always exist or only when debugging?
4) is there a smarter way?

Answers like these: Two questions about max heap sizes and available memory in android seem to imply that there isn't a better way than this?

I know that using less memory is a good thing, and I am. I'm interested to know how to code defensively, here. Seems weird to just wait for an exception to know that you're out of memory.

Thanks!

Community
  • 1
  • 1
lacinato
  • 421
  • 5
  • 9

1 Answers1

0

you may refer to this link.. A complete reserach on the same problem you are facing. OOMRESEACH