1

I'm trying to crack down on a memory leak which is causing the heap to run out of memory and the GC to get invoked. A lot.

Logcat is filled with messages like this:

11-27 20:54:39.052: DEBUG/dalvikvm(32167): GC_CONCURRENT freed 405K, 5% free 11046K/11591K, paused 2ms+3ms

I used the DDMS Allocation tracker to see if there was anything in particular getting allocated more than usual, and the instant that I clicked the 'Get Allocations' button in Eclipse, the list was filled with java.util.AbstractList$SimpleListIterator They get allocated during the main game loop, so there are lots, and I believe this is what is eating the heap memory.(there are approx. 509 allocations of SimpleListIterator)

However, DDMS is telling me that they are being allocated wherever there is a for-each loop, which I think is strange. o_O

Normally I would use a regular for loop, but this article recommends using the for-each loop instead since it's faster, but it mentions nothing about its memory usage

Could all these allocations be the result of using the for-each loop instead of the regular for loop? (It'll take a while to go in and change every single for-each loop in my app, so I'd rather ask first) If they are, then is this normal? Is it a programming error on my part?

UPDATE I changed the for-each loops to regular for loops and the completely solved the problem. I still think its dumb though that I had to do that

AlexRamallo
  • 637
  • 1
  • 5
  • 22

1 Answers1

0

Big Fat Disclaimer: this is not really an answer (maybe). I don't know about this topic beyond what I've read. I'd rather post this as a comment, but the size limitation and formatting features makes an answer a better, constructive, alternative. I don't understand this subject enough to answer, and I'm helping the research only.

On the Android dev guide:

With an ArrayList, a hand-written counted loop is about 3x faster (with or without JIT), but for other collections the enhanced for loop syntax will be exactly equivalent to explicit iterator usage.

Here, a user benchmarked a for-each loop:

n.b. I did notice that using for String s: stringsList was about 50% slower than using an old-style for-loop to access the list. Go figure... Here's the two functions I timed; the array and list were filled with 5000 random (different) strings.

Below, another user warns about benchmarks. Personally, I doubt the use of a for loop would create such a problem on your code, as I've been wary of micro-benchmarking in Java. For what I've seen, it all boils down to microseconds of the Iterator object running. I believe you're doing something wrong, as just because you removed the enhanced for and it performed better doesn't imply that the enhanced for itself is slow or bad.

On another topic:

TL;DR: enhanced loops are indeed slower than a traditional index-based loop over an arraylist; but for most applications the difference should be negligible.

See a doc about the enhanced for loop. You shouldn't be changing the list at will while in the loop. If that relates to your problem, I don't know. Perhaps maybe if I see the code and log lines.

So, I'd guess you're doing something wrong on your code. :-) But it would be better to wait until experienced people come up here.

Community
  • 1
  • 1
davidcesarino
  • 15,491
  • 15
  • 66
  • 108