Hi!
I am building a simulation engine that creates and uses a couple hundred thousand objects per simulation trial, many of which are only used for a small amount of time. Not unexpectedly, I am running into out of heap memory errors because either a) I am not properly dereferencing them, or b) Java's garbage collection is working slower that my object creation. This happens over the course of about 20 trials, I need to run about 100 trials, and I have set the Java heap size to about 256mb.
Regardless of the reason for the problem, and I admit it is likely to be programming errors on my part, I would like to be able to recycle all objects, when no longer needed, in about 30 classes, and make them available for reuse at the end of each trial. All objects in the recycled list could have their references reset, and since these objects are only referred to by others of these objects, my objectives would be met.
With code such as the code below customized for each class, I am able to build a list of all objects in each :
public static AbstractEntity firstRecycledEntity = null; public Entity newEntity(int index, String name) { Entity entity = null; if (firstRecycledEntity == null) { entity = new Entity(index, name); } else { entity = firstRecycledEntity; firstRecycledEntity = firstRecycledEntity.nextRecycledEntity; } return entity; } public static Entity void recycle(E entity) { entity.nextRecycledEntity = firstRecycledEntity; firstRecycledEntity = entity; }
However, I was wondering if there was any way I could do this with generics so that I did not have to copy this code to each class.
Thanks . . .
Phil