I'm working at a pdf reader app by using the native library from muPDF. Unfortunately I'm encountering an OOM error after a lot of swiping through the pages. I'm using the following code to get some insights in the memory behavior (I've also used the Android Device Monitor before):
Runtime.getRuntime().totalMemory() // amount of allocated memory in the app heap
Debug.getNativeHeapAllocatedSize() // amount of allocated memory in the native heap
Now comes the strange thing...due swiping through the pages the returned values of these to methods are always stable, so actually it doesn't look like there would be a memory leak at all. But after some time an OOM error occurs although the values of these two methods are still like at the beginning.
Does anyone know what is happening here?
You can try to prevent OutOfMemoryError from occurring by adding this in your application tag in your AppManifest.xml:
android:largeHeap="true"
Related
I am new to android Profiler and still a noob at memory management. At first, I am having a notifications from LeakCanary so I fix them and successfully removed the leakage so the LeakCanary stops sending me notifications. But when I use the android Profiler I just noticed that the memory usage was rising bit by bit when I open a certain activity and then destroy it using finish(); method. Below are the sample screenshots of my Profiler when I open and close the OrdersActivity.
Here is the SS of total memory used before opening and closing the OrdersActivity
And here is the SS of total memory used after opening and closing the OrdersActivity
Also here is the stacktrace of the LeakCanary
I want to know if this occurrence of rising memory is normal? If not, is there a way to stop this? I am willing to show my codes If you need them in order to help me. I just didn't include it because it was too long.
LeakCanary will detect memory leaks, in the sense of objects that remain in memory when the activity is closed.
If you want to understand why memory usage is increasing as an activity is running, you can track allocations using the profile.
Edit: Also, it's hard to read a screenshot, but it seems to me that was is increasing when you start the activity is code (Code: Memory that your app uses for code and resources, such as dex bytecode, optimized or compiled dex code, .so libraries, and fonts.). In that case, that's perfectly normal: starting an activity need to load extra code (the activity itself, which is probably small, and all of required AppCompat if it hasn't been loaded yet). The usual strategy to test against memory leaks is to start and close the activity hundred times, and verify there is no memory increase between the 2nd time and the 100th time.
For the few days, I've been trying to find out why my app allocates massive amounts of memory and crashes. I'll open a few layouts that admittedly is not optimized. But I cant figure out why sometimes StartActivity() will allocate up to 80MB at a time, and why that memory wont release when I close the activity.
I created a test application filled with images and two layouts that call each other. Using Xamarin Profiler, I came across this memory dump, and I cant figure out what is calling this and how can I have it called more often for my real app?
Please, any insights would be lovely.
I cant figure out what is calling this and how can I have it called more often for my real app?
The Memory Profiler in Android Studio can help you to see how your app allocates memory over over time. It could also be used for xamarin android app.
And try to use IComponentCallbacks2 interface and implement Application.OnTrimMemory to incrementally release memory based on current system constraints.
You could refer to Manage your app's memory for more information.
I am trying to optimize my app which is extensively using Mapview i.e. lots of ItemizedOverlay. While trying to see an app on allocation tracker after using an app for some time I am getting below view.
Few other information,
App is extending MapActivity. App is having fix orientation.
App is not opening any other activity (activity back and forth is not
happening).
Map is showing correctly on the activity.
I have seen multiple post on memory management, seen this Google IO video as well but could not address this problem.
App is running on ICS Tab.
My questions are,
Please see the Allocation in column and android_map_conflict.. value, does it suggest that it is the case of MapView + key conflict? What should be work around in such case?
Size of the memory allocation under android_map_conflict... is keep on increasing. any work around or higher level suggestion? I know it might not needed, but any forceful GC would work? At which place?
I'm not sure that I full understand the output of allocation tracker, but I believe it only refletcts the allocated memory, which can later be released and recovered by the GC. So, if you don't have a memory leak (a object that stills referencerd longer then needed) the allocated memory will be returned to free memory.
If you are experiencing issues with memory leak, they may be coming from somewhere else as well. I suggest that you install the MAT as shown in the video you refer. I've done so, using the version Eclipse plug-in, and it worked like a charm. I found the leak in a couple of minutes (after spending a couple of hours, trying to understand how MAT should be used :-) )
good luck
As I navigate through pages in my android app, the app crashes with the Out Of Memory Exception, it basically happens sometimes, I have tried with finish() method. Also, the images used in my app are of the relevant size, means no extra space occupied. Need a permanent solution to get rid of this Exception.
Looks like there is a memory leak in your app.
Use an allocation tracker to see if any objects are growing over a period of time
http://android-developers.blogspot.in/2009/02/track-memory-allocations.html
My program runs just fine and then I always encounter a random crash error (eg. Error inflating class ) which is almost always started by a OutOfMemoryError.
I watched my VM Heap in DDMS and it looks like gc is doing it's job as I'll watch the %used go up and down along with the #Objects
I've noticed that I get the crash when I have close to these stats:
Heap Size 11.820 MB
Allocated 7.020 MB
Free 4.801 MB
% Used 59.39%
# Objects 148,927
This is about the peak of usage on the device. Sometimes it crashes here, but other times it allows me to move around to another category which allows GC to do it's work getting the %used down to 32%. However, I usually crash shortly after even though usage is down.
I typically crash out with a chain of errors. The top-most Caused by: error is usually something like Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
I'm not sure where to go next as the device seems fine on resources when it crashes, but complains about not having enough memory. I've tried running the program without images and I still get an app that crashes. Regardless, where should I start looking to find the culprit?
** EDIT **
Kindle Fire on Android 2.3.4 via Eclipse
In pre-honeycomb android versions bitmap data stores outsize VM heap, so its impossible to track bitmap data via DDMS :)
There are two possible reasons:
1. Your interface use giant count of images
2. You create bitmaps manually and memory leak occurs. In one Google IO presentation was said that several garbage collections necessary to completely free bitmap memory
Sorry, missed that app crashed without images. Try dump memory usage periodically and see it later via Eclipse Memory Analyzer