Android: Why is it so long to start a new activity?

I have an Android activity BrowseActivity

that downloads about 3000 records (about 100 bytes) from a database into memory and shows them in ListView

. In AVD, it takes 3 to 6 seconds, which is good.

The problem arises when I want to start a new activity from this BrowseActivity

. It takes about 30 seconds, even to open the most trivial activity. The Dalvik VM appears to be doing a lot of garbage collection for an unknown reason. Please check below logcat

.

01-30 02:06:27.025: D/dalvikvm(553): GC_FOR_MALLOC freed 3889 objects / 221200 bytes in 45ms
01-30 02:06:28.234: D/dalvikvm(553): GC_FOR_MALLOC freed 2784 objects / 76864 bytes in 48ms
01-30 02:06:29.614: D/dalvikvm(553): GC_FOR_MALLOC freed 2665 objects / 70088 bytes in 64ms
01-30 02:06:30.915: D/BrowseActivity(553): Low memory status: false
01-30 02:06:30.915: D/BrowseActivity(553): Low memory threshold (KB): 16384
01-30 02:06:30.915: D/BrowseActivity(553): Memory available (KB): 451348
01-30 02:06:31.095: D/dalvikvm(553): GC_EXTERNAL_ALLOC freed 2711 objects / 85904 bytes in 62ms
01-30 02:06:49.705: D/dalvikvm(553): GC_FOR_MALLOC freed 8894 objects / 600272 bytes in 89ms
01-30 02:06:50.414: D/dalvikvm(553): GC_FOR_MALLOC freed 10757 objects / 730720 bytes in 68ms
01-30 02:06:51.105: D/dalvikvm(553): GC_FOR_MALLOC freed 10251 objects / 694864 bytes in 67ms
... and about 30 more garbage collection messages

      

I've already experimented with fewer records: 1 and 300. This results in a significant number of garbage collection messages (1 and 7 respectively) and a much faster load (1 and 6 seconds respectively), but the memory availability difference is pretty trivial:

01-30 02:03:35.295: D/BrowseActivity(491): Low memory status: false
01-30 02:03:35.295: D/BrowseActivity(491): Low memory threshold (KB): 16384
01-30 02:03:35.295: D/BrowseActivity(491): Memory available (KB): 453184

01-30 02:04:53.494: D/BrowseActivity(522): Low memory status: false
01-30 02:04:53.494: D/BrowseActivity(522): Low memory threshold (KB): 16384
01-30 02:04:53.494: D/BrowseActivity(522): Memory available (KB): 453152

      

It looks like the slow load load times are caused by the GC overhead. My question is, why is Dalvik VM doing a lot of GC? Even with 3000 entries, I believe it will only take 500KB in memory. If it doesn't come with GC overhead, what are the other possible reasons for this slow load?

For the record, I logged memory availability using the information provided by the built-in ActivityManager.MemoryInfo class .

+3


source to share


1 answer


I don't think you have a link to an Activity with 3000 entries in it and this causes the GC to be activated when the activities change.

If you're using an MVC type pattern and have a controller with a reference to each operation, I don't think the GC will clear those entries. The GC is triggered when a section of memory exists, but there is no known reference to it, as soon as you switch activities, the activity reference and all of its functions and member variables are no longer used and therefore must be cleared.

How the GC behaves can also be hardware dependent, meaning if the system has a lot of available memory, it might work differently if it doesn't.

I have an application with more than 3000 records loaded, the controller has a link to each action and so the data in the adapters to view the list is not cleared.



As far as overhead is concerned, it doesn't have to be GC - but it looks like it will contribute to the overhead. The only additional factor would be the time taken to render the components on the screen, etc.

Hope it helps!

~ Dan

+1


source







All Articles