25

I'm working on an app to streaming music from internet... My app does many things and it's structured in this way: I have a tab view and every view is allocated in memory so every time I navigate through tabs I find again the previous status ( every tab can also open a webview to find information about songs, news etc in internet ).. all that grows memory occupation but makes the app very user friendly... After having paid attention to avoid memory leaks following the Android guide, I tried to look at the heap occupation and I found that my app allocates max 3.5MB of memory and the heap size allocated is 4.5 - 4.6 MB... I'm working on the emulator .. They are not so much I think, but sometimes my app is restarted founding in LogCat a strange message like

Grow heap ( frag case ) to 3.373 for 19764-byte allocation

What is it? an emulator issue? or something else? Am I using too much memory?

Thank you in advance for any help :)

Erenwoid
  • 983
  • 6
  • 13
  • 25

1 Answers1

36

The maximum heap size depends on the device (you can get that value by calling Runtime.getRuntime().maxMemory()), but it's probably around 32MB. In order to save memory, Android doesn't allocate maximum memory to every app automatically. Instead it waits until the app need more memory and then gives it more heap space as needed until it's reached the max. I believe that's the Grow heap message you see.

If you do a lot of memory allocation and freeing, you may run into fragmentation problems. Wikipedia has a decent description here, but basically means that you might have the required memory available, just not all in one chunk. Hence the need to grow the heap.

So to answer your questions, it's probably not an emulator issue, it's just the nature of your program, which sounds a little memory heavy. However this isn't a bad thing. I don't think using 3-5MB for multiple tabs with webviews is too much.

Steve Blackwell
  • 5,904
  • 32
  • 49
  • oh thanks for the answer ^^ why it's not a bad thing? my app exits when this happens... I think this is bad... my fault? I receive another message: heapworker is wedged: 13129ms spent inside Landroid/media/MediaPlayer;.finalize()V ... Do you know what is this? – Erenwoid Aug 31 '11 at 01:20
  • Yeah, if your app is exiting, then that's bad. I meant using just ~4MB of heap for multiple web pages is not bad. I don't know too much about the `HeapWorker` issue. Usually it keeps track of allocated memory and runs the garbage collector on unused objects. My guess is that one of your threads is using all the CPU, and the `MediaPlayer` isn't given the time to run `finalize()`, which is a function that should definitely not take 13 seconds. – Steve Blackwell Aug 31 '11 at 19:26