I'm writing an app that generates a fair amount of garbage but nowhere near enough to hit the memory limit. That is, even though garbage is generated, there is no need to collect garbage. However, in spite of this, and in spite of the fact that a very small percentage of the total memory available is being used, the garbage collector is constantly being called and lagging my application.
As you can see from this output, I'm using only 2% of my available memory and yet the garbage collector is constantly running:
12-22 04:36:08.219: D/dalvikvm(12033): WAIT_FOR_CONCURRENT_GC blocked 61ms
12-22 04:36:08.359: D/dalvikvm(12033): GC_CONCURRENT freed 1012K, 98% free 11865K/405196K, paused 4ms+13ms, total 73ms
12-22 04:36:08.359: D/dalvikvm(12033): WAIT_FOR_CONCURRENT_GC blocked 38ms
12-22 04:36:08.479: D/dalvikvm(12033): GC_CONCURRENT freed 1020K, 98% free 11862K/405196K, paused 3ms+13ms, total 66ms
12-22 04:36:08.479: D/dalvikvm(12033): WAIT_FOR_CONCURRENT_GC blocked 36ms
12-22 04:36:08.649: D/dalvikvm(12033): GC_CONCURRENT freed 1015K, 98% free 11863K/405196K, paused 2ms+16ms, total 78ms
12-22 04:36:08.649: D/dalvikvm(12033): WAIT_FOR_CONCURRENT_GC blocked 66ms
12-22 04:36:08.789: D/dalvikvm(12033): GC_CONCURRENT freed 1017K, 98% free 11861K/405196K, paused 2ms+13ms, total 66ms
12-22 04:36:08.789: D/dalvikvm(12033): WAIT_FOR_CONCURRENT_GC blocked 58ms
Avoiding this unnecessary garbage collection entirely will make or break my app.
Why is the garbage collector running if I have plenty of free memory? Is there any way I can keep the garbage collector from running until absolutely necessary? I should also mention I'm running on a rooted phone.
Thanks!
Edit: Ugh.. I've been trying to eliminate all garbage using factories and various techniques but there's still a fair amount being generated and I think it's due to some string concatenation of the form s1 + s2 + s3 . I've converted such expressions into forms where I can save references to each component but I'm not sure it's air-tight. Do you see any garbage being generated by the following lines?
String ss1 = input.substring(m, m+k1);
String ss2 = ss1 + possible_perturbations1.charAt(p1);
String ss3 = input.substring(m+k1+1, m+k2);
String ss4 = ss2 + ss3;
String ss5 = ss4 + possible_perturbations2.charAt(p2);
String ss6 = input.substring(m+k2+1, m+i+1);
String current_str = ss5 + ss6;
allStrs.add(ss1);
allStrs.add(ss2);
allStrs.add(ss3);
allStrs.add(ss4);
allStrs.add(ss5);
allStrs.add(ss6);
allStrs.add(current_str);
A couple notes: the remove functions of most of the standard library containers generate garbage, as do most binary operations between objects that generate temporary variables such as Strings, Integers, Doubles, etc... Obvious to most, I guess, but still good to make clear.