Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8226159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:40:06+00:00 2026-06-07T15:40:06+00:00

This question involves memory management in Java for performance reasons: because I am developing

  • 0

This question involves memory management in Java for performance reasons: because I am developing this program as an Android Game and memory GC’s kill my performance. So I have done a large amount of work so far and it turns out that I am doing a great job of optimizing the memory usage of my game, but I have one problem: iterators!

Here is what I am doing:

  1. Start game level.
  2. Start allocation tracker (this way we ignore all of the allocations that will remain for as long as the level runs; I have many objects that only get created once at the beginning of the level and they are not the problem).
  3. Do a few things in the level and get the allocations.

My allocations are full of this:

466 24 java.util.AbstractList$SimpleListIterator 12 java.util.AbstractList iterator
465 24 java.util.AbstractList$SimpleListIterator 12 java.util.AbstractList iterator
464 24 java.util.AbstractList$SimpleListIterator 12 java.util.AbstractList iterator
463 24 java.util.AbstractList$SimpleListIterator 12 java.util.AbstractList iterator
461 24 java.util.AbstractList$SimpleListIterator 12 java.util.AbstractList iterator
456 24 java.util.ArrayList$ArrayListIterator 12 java.util.ArrayList iterator
454 24 java.util.ArrayList$ArrayListIterator 12 java.util.ArrayList iterator
453 24 java.util.ArrayList$ArrayListIterator 12 java.util.ArrayList iterator
452 24 java.util.ArrayList$ArrayListIterator 12 java.util.ArrayList iterator

So the only objects that are being allocated while my game is running are iterators! Okay, well now to fix it then…what code is causing the problem I asked…here it is:

for (Segment side : listOfSides.getSides()) {
    // do stuff
}

Yes, it turns out that for-each syntax calls iterator behind the scenes to populate each element. Which makes perfect sense and exactly what I expected it to do but I did not realise that it could build up so horribly and cause a performance problem for games. If I could get rid of this problem then it would really make my game run like lightning no matter what phone it was on. So my question is: what would you do to make it so that all of these temporary iterators were not created and then immediately discarded resulting in nasty GC runs? Bonus points for doing so in a way that does not make my code completely ugly! (And using ndk on Android is not an option)

P.S. I was thinking that for all of my ArrayLists I could start using the get(int i) function as they are arrays behind the scenes and the integer I would use to index that would be placed on the stack and not the heap. But for the other objects like a HashMap and LinkedList I am not sure what to do.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T15:40:09+00:00Added an answer on June 7, 2026 at 3:40 pm

    ArrayList and LinkedList let you traverse the elements using get(int i) (note it might be slow for LinkedList, I don’t know how get() is implemented.) This is the recommended approach to avoid allocating iterators. If you look at the source code of the platform you will notice that we try to avoid using the for-each syntax as much as possible.

    For HashMap you can grab the underlying sets using entrySet() and then call toArray(Object[]) and pass a pre-allocated array big enough to hold all the values. Alternatively, see if you could use the various SparseArray classes offered by Android.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read almost every Question here on SO about memory management that involves NSStrings,
This question involves 3 tables and 1 form in my Access database. The tables
My question involves this simple walkthrough shown in the article Preserve Size and Location
I'm not sure what to call this question, since it involves a variety of
This is more a general question but my particular case involves a ruby/rails app
This is mostly a data warehouse philosophy question. My project involves an Oracle forms
This question is really basic. What is the performance difference between removing a UIView
This a VERY open question. Basically, I have a computing application that launches test
I've followed this video series on WCF and have the demo working. It involves
This questions involves 2 different implementations of essentially the same code. First, using delegate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.