2
I would like to better understand what Java memory management does in the following situation.
Knowing that I am suffering from performance problems, I am trying to take the utmost care not to make the situation even worse, in that occurred the need to perform the following method:
public List<List<Object>> getValues() throws DataNotFoundException {
List<List<Object>> values = new ArrayList<>();
for (SurveyActivity surveyActivity : this.surveyActivities) {
this.recordsFactory = new SurveyActivityExtractionRecordsFactory(this.surveyForm, this.headersFactory.getHeaders());
List<Object> resultInformation = new ArrayList<>();
this.recordsFactory.getSurveyBasicInfo(surveyActivity);
this.recordsFactory.getSurveyQuestionInfo(surveyActivity);
resultInformation.addAll(new ArrayList<>(this.recordsFactory.getSurveyInformation().values()));
values.add(resultInformation);
}
return values;
}
Note that the attribute recordsFactory
is receiving a new instance for each entry in the loop, this decision-making should be, because we need to "clear" the object instance for each entry in the loop, as the Java memory manager will handle this situation?
It will allocate this new object in the same memory space previously?
Knowing that I need to "clean up" this instance, there is a better alternative?
Is there any article where I can find more information about how the memory manager works?
Because it says "(...) this decision-making is necessary, because we need to "clean" the object instance for each entry in the loop (...)". I ask because it seems to me that values passed to the constructor do not change during the loop. So, the object recordsFactory instantiated has a state equal to the previous one ("is always the same"). Unless
getSurveyBasicInfo()
andgetSurveyQuestionInfo
change the state which would be "wrong".– ramaral
@ramaral inside the Surveyactivityextractionrecordsfactory class there is a map with values that for each entry in the loop I need to ensure that there will be no garbage, using the same instance, there were cases where incorrect values appeared. At that point then I made the decision to perform a new for each entry in the loop, my question turned out to be whether this decision making ended up affecting the performance.
– Emanoel
Without knowing the class it is difficult to speak. However it seems to me that you should rethink its implementation. The fact of having methods called
getQualquerCoisa
that return void is an indicator that something could be improved.– ramaral
Some doubts are still blank, but I will assess your response as positive.
– Emanoel
@Emanoel say what.
– Maniero