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();
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 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
Anyone knows what is Android function to get my app's allocated memory info as shown in DDMS?
It is the value highlighted by yellow:
Thanks
So I found correct function:
Runtime rt = Runtime.getRuntime();
long heapSize = rt.totalMemory();
long free = rt.freeMemory();
long allocated = heapSize - free;
This gives exact numbers reported by DDMS.
This is to get Allocated Memory Debug.getNativeHeapAllocatedSize()
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...
In my Android developer console, I see some crashes with
Caused by: java.lang.OutOfMemoryError: (Heap Size=5795KB, Allocated=3859KB)
Especially the small Heap Size (less than 6MB) surprises me.
How do I have to interpret this Heap Size? Is this the size of the heap prior to the OOM call? Can I assume that the max heap is at least 16MB? If so, does this mean that I tried to allocate a block of at least 10MB (16MB - 6MB) ?
You can use the following to check the memory status:
ActivityManager actMgr = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo minfo = new ActivityManager.MemoryInfo();
actMgr.getMemoryInfo(minfo);
if(minfo.lowMemory) { //do something
}