Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android dalvikvm-heap: Clamp target GC heap

Tags:

I am writing a procedure to sync large number of contacts with the Android Contacts database. The downloading works fine for about 700 contacts after which i am consistantly getting a memory heap error which calls infinite number of GC statement and ends up Re-booting the Phone. I am facing the problem on HTC desire.

I checked heap size of the application using the Heap alocation tool from DDMS as well as extracted the hprof file using Debug.dumpHprofData. Both the logs indicated that the Heap size is about 2.4MB.

However i get the following logs which indicates that the heap size is more that 32.MB

dalvikvm-heap(92): Clamp target GC heap from 33.999MB to 32.000MB dalvikvm(92): GC_FOR_MALLOC freed 2 objects / 48 bytes in 313ms 

I had inserted following log statements in loop where my contatcs downloading logic was written.

Log.e("Memory", "free mem =" +runtime.freeMemory()); Log.e("Memory", "total memory =" +runtime.totalMemory()); 

These are the initial and final values of the statement printed

--------------------------------------------------------------- 11-11 12:56:04.168: ERROR/Memory(25132): free mem =871248 11-11 12:56:04.168: ERROR/Memory(25132): total memory =4202464  ---------------------------------------------------------------  11-11 13:01:55.408: ERROR/Memory(25132): free mem =891640 11-11 13:01:55.408: ERROR/Memory(25132): total memory =4726752  --------------------------------------------------------------- 

This indicates that apperently there are no memory leaks present in the syncing contacts logic.

Can someone please let me know for why is the heap size increased (upto 32.00Mb) to such a extent that the device re-boots itself? I am new to Android and Java so please go easy on me :).....

like image 427
Manish Khot Avatar asked Nov 11 '10 14:11

Manish Khot


2 Answers

While this isn't the best answer I'd highly recommend you watch the video of the Memory management for Android Apps talk at Google IO 2011. It does a great job explaining how to manage memory and what the messages you are seeing actually mean.

like image 117
slayton Avatar answered Nov 07 '22 18:11

slayton


You need to post your code for anyone to help. Otherwise i assume two things:

  • Since your phone is crashing, you must be doing something awesome which allows your program to run outside of the VM's allocated memory space.
  • Are you storing all of the contacts in a List or Array? If so, there's your problem. This is what Streams are good at fixing.
  • like image 45
    Paul Nikonowicz Avatar answered Nov 07 '22 17:11

    Paul Nikonowicz