My question is rather specific, and hence difficult to search for. I am programming Java and learning the language. I have designed one class, called Experiment, in which I define common functions for all experiments, and default parameters.
For each specific experiment, say ExperimentA, I then subclass Experiment. Each time I want to run experiment A, I make an instance of ExperimentA, passing parameters to the constructor which then overrides the default parameters defined in Experiment. I have two questions. I posted both questions in the same post because I thought they might be related.
1) Is the design above reasonable? Initially I wanted to use interfaces, but I found out that values in interfaces must be constant, and it would therefore be impossible to override parameters.
2) I am having a problem of memory leaks when running multiple subsequent experiments of the same type with changed parameters.
for(int na:ArrayOfIntegers) {
Experiment e1 = new ExperimentA(na,otherArgs);
Experiment.runExperiment(e1);
}
For some reasons, objects that were retained by the e1 object are kept alive in the heap even after e1 has been assigned to a new ExperimentA object. I know this is a very general question, but I am new to Java, and some guesses as to what the problem might be could help me.
The only malpractice that I've been doing that I am aware of is making the default variables in Experiment public, instead of private and using getters, but I can't see how that should cause any problems in this context. Basically there are no references to the e1 object after the runExperiment() function has been executed and another ExperimentA object is created. Any ideas?