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 8211479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:26:25+00:00 2026-06-07T10:26:25+00:00

I am currently writing a game for android where there are enemies that fly

  • 0

I am currently writing a game for android where there are enemies that fly across the screen and then disappear, to be replaced by other enemies. Now, this happens very fast, and my code currently performs a lot of memory allocation and deallocation to create and delete these enemy objects, so I’m trying to find a way to optimize this. I got this Pool class implementation from a book on android game dev:

public class Pool<T> {
    public interface PoolObjectFactory<T> {
        public T createObject();
    }
    private final List<T>               freeObjects;
    private final PoolObjectFactory<T>  factory;
    private int                         maxObjects;

    public Pool(PoolObjectFactory<T> factory, int maxObjects) {
        this.maxObjects = maxObjects;
        this.factory = factory;
        freeObjects = new ArrayList<T>(maxObjects);
    }

    public T newObject() {
        T object = null;
        if (freeObjects.isEmpty()) {
            object = factory.createObject();
        } else {
            object = freeObjects.remove(freeObjects.size() - 1);
        }
        return object;
    }

    public void free(T object) {
        if (freeObjects.size() < maxObjects) freeObjects.add(object);
    }
}

Now, the way to use this class is as follows:

PoolObjectFactory<Enemy> factory = new  PoolObjectFactory<Enemy>() { 
    public Enemy createObject() { 
        return  new  Enemy(); 
    } 
}; 
Pool<Enemy> enemyPool = new  Pool<Enemy>(factory, 50); 

The obvious problem with this method is that you can’t input any parameters to the createObject() method, thus forcing you to use classes that take no arguments in their constructor. This will force me to rewrite a lot of code since the Enemy class I’m using takes several different parameters. I can think of a couple of workarounds, like this one:

PoolObjectFactory<Enemy> factory = new  PoolObjectFactory<Enemy>() { 
    public Enemy createObject(Object... args) { 
        return  new  Enemy((Float)args[0], (Float)args[1]); 
    } 
}; 
Pool<Enemy> enemyPool = new  Pool<Enemy>(factory, 50); 

But it’s error-prone and annoying to update. I could also initialize the Enemy object in the createObject() method with bogus values and then set them manually later, or I could create a Pool class for every single object but I would really prefer not doing that.

Any suggestions on how to improve this code? How do you fellow java game developers deal with pooling objects to avoid garbage collection? Thanks very much.

  • 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-07T10:26:26+00:00Added an answer on June 7, 2026 at 10:26 am

    1) You should override the createObject function in your PoolObjectFactory.

    2) You will need a initialize() function that actually sets the parameters for each EnemyObject. Just have the constructor for the EnemyObject call the initialize function. Then, when you get the object out of the pool, you should just call initialize with your parameters and it should work perfectly.

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

Sidebar

Related Questions

I am writing a somewhat complex game engine in Android. Currently I have a
I'm currently writing my own 2D Game Engine for a game that I would
I am currently writing a online game. Now I have to check if an
I'm currently writing a game that uses 2D OpenGL output under sdl, and I'm
I am currently writing a game that finds an inputted word in matrix in
I'm currently writing a game of immense sophistication and cunning, that will fill you
I am currently working on writing small game that is written in javascript, alot
I'm currently writing an AI for a game that is written in c++. The
i'm currently writing a game and now encountered a issue. in my game, if
I'm currently writing a very simple game engine for an assignment and to make

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.