Android Lowest Memory Amount
I have been debugging my app and checking the memory allocation and came across, what I think, a curious thing. Often a question is asked how to manage memory best and so on, my question is what is the lowest default memory boundary in a simple application? So I went and created a simple hello world project for level 8 (2.2) API that simply has Hello World written in it.
Then I had a look in the DDMS in the Heap tab. Allocated was 2.318 MB, 510 Kb Free (82% used). So I thought why is it using 82% of allowed(?) heap on a simple Hello World test. Digging deeper I did a heap dump and ran in thr开发者_如何学Cough MAT in eclipse. As a first instance I had a look at the dominator_tree (all objects sorted by size) here is what I found:
TrustManagerImpl (176.616 Retained Heap) - 9.83% - I guess thats important for security
com.ibm.icu4jni.util.Resources$DefaultTimeZones (165.432 Retained heap) - 9.21% - WHY? I had a look at the content of it and it is a single String array with 561 members each one is info about a timezone? I don't need that really do I? AND it takes 10% of the heap.
android.text.Html$HtmlParser (126.592 Retained Heap) - 7.05% - I guess thats a HTML Parser for my TextView that has Hello World. Again though WHY? I'm not parsing HTML so why is the Parser even there? In actual fact the HTMLSchema is what takes up 99.9% of the object.
There is more there but of less significance as I can see (which might be wrong). Anyone has any thoughts about the 3 instances I mentioned above? I mean, am I miss interpreting something here? I don't think the memory usage should be this high. The question that suggests itself if every app has this baseline how does it scale? Probably not well? Should it be fixed, and hence lead to better memory management in Android? I ran this on HTC Desire 2.2.
Alex
Allocated was 2.318 MB, 510 Kb Free (82% used). So I thought why is it using 82% of allowed(?) heap on a simple Hello World test.
Bear in mind that your available heap will range from 16MB to 32MB (or perhaps higher), depending on device.
I don't need that really do I?
Optimizing time zone processing has been evolving for the past two years, trading off RAM for CPU. Preloading this information eliminates unnecessary pauses while the app is running. Furthermore, this is 0.1% of your heap at most.
I'm not parsing HTML so why is the Parser even there?
Presumably for the same reasons the time zones are preloaded nowadays -- trading a bit of RAM to save CPU time during execution. This too is under 0.1% of your heap.
I don't think the memory usage should be this high.
You are welcome to your opinion. I think you wasted a few hours of your life that could have been better spent optimizing your code. One bitmap may use substantially more RAM than do either of these constructs that you feel are so egregious.
精彩评论