What does Java memory management accomplish in an assignment of a variable already allocated in memory previously?

2

I would like to better understand what Java memory management performs 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, it was necessary 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;
}

Notice that the recordsFactory attribute is getting a new instance for each entry in the loop, this decision-making is due, since we need to "clean" the instance of the object for each entry in the loop, such as the Java memory manager will you handle this situation?

Will it allocate this new object in the same memory space previously?

Knowing that I need to "clean" this instance, is there a better alternative?

Is there an article where I can find more information about how the memory manager works?

    
asked by anonymous 25.01.2018 / 15:53

1 answer

4

Every time I use a new you are creating a new object in heap (I do not know if this will change in Java 10, which will have stack ). It's that simple. And every new object will be allocated where it is best at that moment. It will certainly be another location and will push the garbage collector.

If the object allows and no longer needs the data it can clear it (zeroing all data, including the object graph hanging on it) instead of creating a new one. Or at least change all your data, just be careful not to leave trash from the previous one. In complex objects this can be very tricky.

Depending on the case you can think of a completely different algorithm.

In fact, if you have too many items to process this algorithm will do damage to memory.

Some answers are not about Java, but either is the same or gives an idea:

25.01.2018 / 16:29