I have a Native android app which Makes a lot of calls to Malloc. I am trying to allocate a chunk of the RAM and then run some memory intensive tasks to see how my Android device handles it. But I am not sure if the device is taking memory from the RAM or other sources of memory. Is there a way to find out? If it is taking memory from the cache, how do I make it so I am allocating the memory only from the RAM?
But I am not sure if the device is taking memory from the RAM or other sources of memory
malloc() allocates memory from the process' heap, which in turn comes from the device RAM. It has done so for decades.
Related
I have created an Android app and I now want to make sure that it never goes above 16MB of heap usage.
Unfortunately for this task my device has a much bigger heap than the minimum of 16MB, it has at least 32MB.
When I track the allocations it just keeps allocating and allocating and seldom garbage collects which makes it hard to track down memory leaks.
I have tried to use various profilers but it is not easy.
Preferrably I would like to back and forth between activities and just see the heap go up and then back down so that there are no memory leaks but since the garbage collection is postponed until it is really needed this seems hard to do.
Is it possible to limit the heap size to 16MB on a 32MB heap size device for testing purposes?
The best way to do this is to use an Android Virtual Device, as you can configure the heap size in the Android emulator. It is in the advanced options when creating an AVD.
However if you must test this on a real device you can simulate having a smaller heap by creating a memory leak of heap memory on startup. Make sure you keep a strong reference to the object you allocate so it isn't garbage collected.
On a device with 32MB heap size, if you create a 16MB leak on startup this will leave the remaining 16MB of heap space for your application to use.
I've developed a game with libgdx. I've tried to keep memory consumption as low as possible by doing the following:
Use Pooling where possible.
Limit SpriteBatch's size
When I run the game on my phone (Nexus 5x) from Android Studio, my game runs smoothly and it seems that memory consumption is indeed reasonable: The memory consumption is usually under 23MB (I see this via the memory monitor). However, when I go to the memory tab on my phone (Settings --> Memory --> Memory used by apps) I see the following:
A shocking "Maximum memory use" of 1.2 GB for my game. Way above any other app.
My question(s):
What does this number mean? Does this mean that at one point my game took up 1.2 GB of the RAM?
How can that be if the memory monitor never showed a consumption of over 25MB?
Seeing that the game does run smoothly should I try to get this number down? If yes, how can I do this?
You're mixing up native memory (RAM, as shown on the screenshot) and VM memory.
In the memory monitor, you only see the heap (so the memory that was allocated for the VM on which your app is running), but not the actual RAM. This maximum heap size is limited by the system (see e.g. https://stackoverflow.com/a/9940415/1096567 for examples).
Libgdx is using OpenGL via a JNI and is therefore also using the RAM directly.
To investigate your RAM usage in general you need other tools than the Memory Monitor, see e.g.:
How do I discover memory usage of my application in Android?
I don't know your app, but 1.2GB seems a bit high. If you're not using Texture Atlases & the Texture Packer, it's probably a good place to start optimizing. Also be sure to dispose all Disposables (like BitmapFont). Here is a detailed list.
I am developing an application in that I am getting the Out of memory Exception this is happened due to the insufficient memory. Is possible re allocate the memory once reach the maximum allocate memory.
It is not possible to allocate more memory than is in budget which system gives you.
You can try to put android:largeHeap to your manifest, but it works only on some devices and it isnt generally best solution.
Instead you should find out where you are using all that memory and try to optimize to cut it down.
On modern devices there should be plenty of memory and should be enough for most operations with good design. System places gap on memory usage to prevent poorly designed apps, from depleting memory pool.
In my application I use quite a lot of assets to render. This caused my application to crash with an exception indicating that there is no more memory left (when allocating a byte array). Using meminfo I've seen that my process uses about 40mb of memory which according to my calculations is correct (so no hidden excessive memory allocation in my code).
The total memory usage on my system is 300mb. My tablet however supports 1gb of memory and I wonder why it throws me an exception at a usage of 300mb. Is there some per process limit that I need to change? Or are there any other things I'm missing about androids memory management?
Add this to androidManifest in application tag
android:largeHeap="true"
to make things work but this will consume more memory hence more gc calls
when I look at my application in a memory utility (like Memory Usage app), it shows my application using 33MB of memory.
If while debugging my app, I go into DDMS and look at the heap, it's taking 4MB.
So at this point, I'm wondering where the other 29MB is being chewed up by. Secondly, my concern is that I could wind up spending a huge amount of time trying to trim that 4MB to something much smaller like say 2-3MB, but that hardly seems like I'm putting a dent in the memory usage. So how do i get that memory usage down much more significantly?
check and discoer it properly..........
http://elinux.org/Android_Memory_Usage
How do I discover memory usage of my application in Android?