I am currently developing an android application and I am using my own device (nexus 4) to debug it. I seem to have troubles regarding the heap size allocated by the Dalvik VM and I hope you can help me on this one.
My application is processing a relatively important amount of data (~3MB) from a file and use it to generate java objects which go around 10MB in total. This should not be a problem for a device which has 2GB of RAM and a heap size limit of 512MB ( if I am not mistaken).
However, the debugging tool tells me that the heap size allocated to my application is roughly 10MB, leading to an outrageous amount of garbage collection (each GC frees ~500KB). I went through already a lot of pages on SO and other forums about Dalvik heap size model, etc and I found the largeHeap="true"
(API level 13+) attribute to use in this case, however, it did not improve anything and I am still stuck with 10MB to use when the device has an initial memory of 2GB. I understand that the OS, etc ,etc take some memory itself, that Dalvik is restricting applications as well, but why can't I reach at least the 512MB?
So, did I misunderstood something? Did I miss something? The problem seems rather simple, but I don't get it.
Your help will be greatly appreciated!
PS: Please don't suggest to switch to the NDK, it is not suitable in my case.
If you're not getting OutOfMemoryError
, altering the heap size won't affect the behavior.
Dalvik's managed heap resists growth in an attempt to minimize the app's memory footprint, and unfortunately will sometimes get "hung up" on a particular size. It's usually possible to kluge around this by allocating a large object (say, a byte[1000000]
) and immediately dropping the reference to it.
FWIW, the best way to guarantee the reference is dropped is to allocate it in a method that returns immediately. Don't just null out a reference. The Dalvik GC is not live-precise, so it won't ignore "dead" references lingering in registers.
(All of the above is for Android 4.4 and earlier. Hopefully the situation will improve in future releases.)