Fake pooling?

Hello guys, I have a doubt about what really does one piece of code.

I have pooling implemented this way (not entirely, just the simple way): http://www.java-gaming.org/topics/object-pooling/27133/view.html

But, I made some little changes.

The “newObject” method of the factory always returns null. Why is that? Because I want to reuse an abstract class which has a lot of sub-classes which implement the abstract methods. Something like this:

abstract BaseClass ____ ConcreteClass1
|____ ConcreteClass2
|____ etc…

Every ConcreteClassN has his own vars and stuff. And I dont want to do a pool for every one of them. I want to make a pool for the abstract class and reuse that “memory space” for a ConcreteClass (any).

So…I “get” an object this way:

BaseClass base= basePool.get();

Remember, the newMethod returns always null. So, if no objects are on the stack, get() will return null (I dont mind).
But lets see when the get() returns something. That something is a ConcreteClass object (don’t know exactly which one of them) that has been recycled somewhere. It uses memory and then what I do, for example, is:

base= new ConcreteClass2(data);

Is this code reusing the memory used by the “base” object which is actually a recycled ConcreteClassN object? Im not very sure lol

(btw, this works as expected)

Thank you guys for the help!

Java stores objects by reference (you could also call them pointers). So all the variables are just references to the objects. When you assign a new object to a variable you are changing the value of that reference so that it points to the position in memory of the new object.

Whilst you are reusing the memory of the reference, that is only 4 / 8 bytes. If you want to reuse memory, you must use mutable classes and have a stack of final (const) instances of the class which you populate with the appropriate data when you need it.

Are You sure You need object pooling ? Or are You doing this as a learning task ?

mmh…I see. So…Im not reusing the memory of the previus object. Too bad. I will try something more then. Just one sec.
Edit: nop, another bad idea that doesn’t work (tried to make an ilegal cast xD).

The problem is that the game will create this objects during gameplay. Over the time, they will be a lot. Thats why Im trying this.

Well the garbage collector in most Java implementations that I know of (that includes Android) are pretty damn descent nowadays. As they said in the comments of the thread you linked, object pooling should be implemented if and when you find the gc is stalling your game.

The possible exception to this is if you’re using OpenGL and need lots of Buffers. Reusing them is a must and pooling is a pretty descent way of doing this.

Unless You talk about several millions of objects, then this is a non-issue :slight_smile:
I believe there was a there was a thread here somewhere where more concrete numbers were mentioned.
Anyway, at least on the desktop, You should more or less never worry about gc.

Thank you guys. You are right. I’ll not worry about GC right now. If this gets bigger and I encounter problems, I’ll try to fix them then.