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
Related
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??"
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/
I got problems about grow heap which I have 2 questions to ask.
First, How can I suppose to check current grow heap in my devices?
Second, How can I decrease my grow heap size to prevent out of memory error?
Do you mean you want to check the total size of the heap your application can use? Or how much free memory is left in the heap?
To see the total size of the heap you could call
Runtime rt = Runtime.getRuntime();
long maxMemory = rt.maxMemory();
Log.v("onCreate", "maxMemory:" + Long.toString(maxMemory));
This will tell you how much memory in bytes your app is allowed to use (source)
To find out how much is left you could do the following
When are you getting out of memory errors? A starting point could be to override onLowMemory() method...
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);
In relation to this topic Android: OutofMemoryError....
How Can you see the heap size on ddms (Eclipse)? I can only track allocations and it doesn't show nothing
You can try from code native heap allocation : Debug.getNativeHeapAllocatedSize()
this shows max heap memory of device
Runtime rt = Runtime.getRuntime();
long maxMemory = rt.maxMemory();
this is for heap required by your app
Runtime rt = Runtime.getRuntime();
long maxMemory = rt.maxMemory();