How to calculate RAM usage in android device? - android

I am trying to calculate the Ram usage when I run my application. I saw a few posts on stackoverflow but cldnt quite understand how they go about it.
Can anyone help me in finding the memory(RAM) usage in my device?

You can calculate available memory and if running on API level bigger than 16 you can get the total memory. From there you can have the memory usage.
I am using the following method in my app. It returns the memory available in MBs.
private void showAvailableMemory(){
MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long availableMegs = mi.availMem / 1048576L;
Log.i(TAG, "Available memory = " + availableMegs + " MB");
}
If you are running on API level > 16, you can use MemoryInfo.totalMem to get the device memory.

This is the best answer:
How do I discover memory usage of my application in Android?
But basically, you should check the USS value:
adb shell procrank -u
Example:
PID Vss Rss Pss Uss cmdline
492 73540K 73336K 51256K 49780K system_server
676 53712K 53008K 31831K 30696K com.android.systemui
13918 46408K 46376K 21050K 19504K com.facebook.katana

Use this code to calculate..
ActivityManager activityManager = = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
MemoryInfo memoryInfo = new MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
Get the available memory from memoryInfo.
Also, to measure Use this tool , which will show you the memory usage of your app
http://www.littleeye.co/

Related

How to Manually Set Android VM Heap Size in Emulator?

How do you manually set the VM heap size for Android Studio emulators?
I'm trying to set it really low to make sure my app will still run without out-of-memory errors even on really old devices.
I did this in the emulator settings:
And added this code to check:
Runtime rt = Runtime.getRuntime();
long maxMemory = rt.maxMemory();
Log.v("onCreate", "maxMemory:" + Long.toString(maxMemory));
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
int memoryClass = am.getMemoryClass();
Log.v("onCreate", "memoryClass:" + Integer.toString(memoryClass));
... but I still get this:
Since I changed the settings, why does it not reflect that by saying "maxMem: 128 memoryClass: 16??"

Usage of heap memory for android app

I used following APIs to check memory that my app can use and got the below results.
Runtime rt = Runtime.getRuntime();
long maxMemory = rt.maxMemory();
Log.v("onCreate", "maxMemory:" + Long.toString(maxMemory));
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
int memoryClass = am.getMemoryClass();
Log.v("onCreate", "memoryClass:" + Integer.toString(memoryClass));
V/onCreate: maxMemory:402653184
V/onCreate: memoryClass:64
But when I launch my app, it shows 5MB allocated and 1 MB free.
I was in assumption that entire 64 MB will be for the app and suppose if 5MB is allocated out of 64MB then 59MB should be free.
And as I progress using the application, the heap size allocated is growing with lot of GC causes in between which is impacting the UI performance.
Could someone make me understand this process.
Will this work this way?
Thanks

MemoryInfo.availMem keeps decreasing

The callback onLowMemory() of my Activity kept on being called and so to investigate the issue, I wrote the following lines of code in the main game loop:
ActivityManager activityManager = (ActivityManager) Activity.getActivity().getSystemService(Activity.ACTIVITY_SERVICE);
MemoryInfo mi = new MemoryInfo();
activityManager.getMemoryInfo(mi);
Log.v("Tag", "Testing mem: " + mi.availMem);
The logs were very interesting: the availMem kept on decreasing. Now to make sure objects were being destroyed properly I I took hprof memory dumps. The objects that were being destroyed, as expected, are not showing in those dumps.
Also, availMem is supposed to be memory available to the whole system and not just my app, which makes it even weirder.
Can anybody help me understand whats going on here. Any help would be much appreciated. Thanks!

Running Memory Size

I have an app which consists of a service and some activities. The running size of app reported by settings is around 15MB which seems excessive to me. Eclipse MAT reports total size as only 2.2 MB in Leak Suspects. My questions are:-
Is my service running size actually only 2.2 MB ?
Is the 15 MB reported by settings something to do with backstack of activities and therefore I need not be worried ?
Thanks
use this code hope this help you
MemoryInfo mi = new MemoryInfo();
ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long availableMegs = mi.availMem / 1048576L;
Log.e("Avaliable start", " "+availableMegs);

Determining available heap space for Activity (preventing OutOfMemoryException)

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!
you may refer to this link.. A complete reserach on the same problem you are facing.
OOMRESEACH

Categories

Resources